INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '短信通知-新', 'dxtz-x', '2', '62', 'select month,type,sum(qty) qty,sum(je) je
from (
select month,''短信发送情况'' type, sum(qty) qty, sum(je) je
  from (select to_char(fcreate_time, ''YYYYMM'') month,
               sum(1) qty,
               round(sum(1) * .045, 2) je
          from iquest.mall_template_msg_log a
         where a.ftype in (3, 10002, 20101, 20102,18301)
           and fis_accect = 1
           and to_char(fcreate_time,''YYYYMM'')<>to_char(sysdate,''YYYYMM'')
         group by to_char(fcreate_time, ''YYYYMM'')
        union all
        select to_char(a.fcreate_time, ''YYYYMM'') month,
               sum(fid_sign) qty,
               round(sum(fid_sign) * .045, 2) je
          from iquest.mall_phone_reg a
           where to_char(fcreate_time,''YYYYMM'')<>to_char(sysdate,''YYYYMM'')
         group by to_char(a.fcreate_time, ''YYYYMM''))
 group by month
        union all
 select ''202002'' month,''短信发送情况'' type,54846 qty,round(54846*.45,2) je from dual
        union all
 select ''202004'' month,''短信发送情况'' type,1097 qty,round(1097*.45,2) je from dual
        union all
 select ''202005'' month,''短信发送情况'' type,23892 qty,round(23892*.45,2) je from dual)
 group by month,type
 order by month desc', 'select month,type,sum(qty) qty,sum(je) je
from (
select month,''短信发送情况'' type, sum(qty) qty, sum(je) je
  from (select to_char(fcreate_time, ''YYYYMM'') month,
               sum(1) qty,
               round(sum(1) * .045, 2) je
          from iquest.mall_template_msg_log a
         where a.ftype in (3, 10002, 20101, 20102,18301)
           and fis_accect = 1
           and to_char(fcreate_time,''YYYYMM'')<>to_char(sysdate,''YYYYMM'')
         group by to_char(fcreate_time, ''YYYYMM'')
        union all
        select to_char(a.fcreate_time, ''YYYYMM'') month,
               sum(fid_sign) qty,
               round(sum(fid_sign) * .045, 2) je
          from iquest.mall_phone_reg a
           where to_char(fcreate_time,''YYYYMM'')<>to_char(sysdate,''YYYYMM'')
         group by to_char(a.fcreate_time, ''YYYYMM''))
 group by month
        union all
 select ''202002'' month,''短信发送情况'' type,54846 qty,round(54846*.45,2) je from dual
        union all
 select ''202004'' month,''短信发送情况'' type,1097 qty,round(1097*.45,2) je from dual
        union all
 select ''202005'' month,''短信发送情况'' type,23892 qty,round(23892*.45,2) je from dual)
 group by month,type
 order by month desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('30', '数据分析-季节', 'sjfx-jj', '5', '82', 'select ID value,ATTRIBNAME label from m_dim WHERE dimflag=''DIM9''', 'select ID value,ATTRIBNAME label from m_dim WHERE dimflag=''DIM9''', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('30', '购物券核销情况', 'gwqhxqk', '2', '63', ' select aa.t_month_id year_month,
       substr(s.name, 0, instr(s.name, ''总'') - 1) name,
       nvl(bb.sum_count, 0) sum_count,
       nvl(bb.verifyed, 0) verifyed
  from (select max(t_month_id) t_month_id, b.id
          from t_day a,
               (select c_customer_id id
                  from C_VOUCHERS a
                 where a.creationdate >=to_date(to_char(add_months(sysdate, -2), ''YYYYMM'') ||''01 00:00:00'',''YYYYMMDD HH24:MI:SS'')
                   and a.c_customer_id<>176
                   and a.c_customer_id<>1140
                   and length(a.vouchers_no) = 12
                 group by a.c_customer_id) b
         where a.id between
               to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and
               to_char(last_day(trunc(add_months(sysdate, -1))), ''YYYYMMDD'')
         group by b.id) aa
  left join (select a.c_customer_id,
                    to_char(a.creationdate, ''YYYYMM'') year_month,
                    count(1) sum_count,
                    sum(decode(a.is_verifyed, ''Y'', 1, 0)) verifyed
               from C_VOUCHERS a
              where length(a.vouchers_no) = 12
                and a.creationdate between
                    to_date(to_char(add_months(sysdate, -1), ''YYYYMM'') ||''01 00:00:00'',''YYYYMMDD HH24:MI:SS'') and
                    to_date(to_char(last_day(trunc(add_months(sysdate, -1))),''YYYYMMDD'') || '' 23:59:59'',''YYYYMMDD HH24:MI:SS'')
                and a.vou_type = ''VOU5''
              group by a.c_customer_id, to_char(a.creationdate, ''YYYYMM'')) bb
    on (aa.t_month_id = bb.year_month and bb.c_customer_id = aa.id)
  left join c_customer s
    on aa.id = s.id
 order by 1, 3 desc', ' select aa.t_month_id year_month,
       substr(s.name, 0, instr(s.name, ''总'') - 1) name,
       nvl(bb.sum_count, 0) sum_count,
       nvl(bb.verifyed, 0) verifyed
  from (select max(t_month_id) t_month_id, b.id
          from t_day a,
               (select c_customer_id id
                  from C_VOUCHERS a
                 where a.creationdate >=to_date(to_char(add_months(sysdate, -2), ''YYYYMM'') ||''01 00:00:00'',''YYYYMMDD HH24:MI:SS'')
                   and a.c_customer_id<>176
                   and a.c_customer_id<>1140
                   and length(a.vouchers_no) = 12
                 group by a.c_customer_id) b
         where a.id between
               to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and
               to_char(last_day(trunc(add_months(sysdate, -1))), ''YYYYMMDD'')
         group by b.id) aa
  left join (select a.c_customer_id,
                    to_char(a.creationdate, ''YYYYMM'') year_month,
                    count(1) sum_count,
                    sum(decode(a.is_verifyed, ''Y'', 1, 0)) verifyed
               from C_VOUCHERS a
              where length(a.vouchers_no) = 12
                and a.creationdate between
                    to_date(to_char(add_months(sysdate, -1), ''YYYYMM'') ||''01 00:00:00'',''YYYYMMDD HH24:MI:SS'') and
                    to_date(to_char(last_day(trunc(add_months(sysdate, -1))),''YYYYMMDD'') || '' 23:59:59'',''YYYYMMDD HH24:MI:SS'')
                and a.vou_type = ''VOU5''
              group by a.c_customer_id, to_char(a.creationdate, ''YYYYMM'')) bb
    on (aa.t_month_id = bb.year_month and bb.c_customer_id = aa.id)
  left join c_customer s
    on aa.id = s.id
 order by 1, 3 desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('30', '新增会员', 'xzhy', '2', '41', 'select months,name,sum(bl) bl,max(months1) months1,max(name1) name1,sum(bl1) bl1
from (
select nvl(a.months, b.months) months,
       nvl(a.name, b.name) name,
       nvl(a.bl, 0) bl,
       nvl(b.months, a.months) months1,
       nvl(b.name, a.name) name1,
       nvl(b.bl, 0) bl1
  from
 (select substr(a.OPENCARDDATE, 1, 6) months,
         case
           when instr(c.name, ''总'') > 1 then
            decode(substr(c.name, 1, instr(c.name, ''总'') - 1),''温州'',''新南昌'',substr(c.name, 1, instr(c.name, ''总'') - 1))
           else
            substr(c.name, 1, 3)
         end name,
         count(a.id) bl
    from c_client_vip a, c_customer c
   where a.isactive = ''Y''
     and a.c_customer_id = c.id
     and c.C_CUSRANK_ID = 2
     and C.isactive = ''Y''
     and c.IS_ONLINE = ''Y''
     and a.OPENCARDDATE between to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -2)), ''YYYYMMDD'')
   group by substr(a.OPENCARDDATE, 1, 6),
            case
              when instr(c.name, ''总'') > 1
                then decode(substr(c.name, 1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
              else
               substr(c.name, 1, 3)
            end) a
  Full outer join
  (select substr(a.OPENCARDDATE, 1, 6) months,
          case
            when instr(c.name, ''总'') > 1
              then decode(substr(c.name, 1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
            else
             substr(c.name, 1, 3)
          end name,
          count(a.id) bl
     from c_client_vip a, c_customer c
    where a.isactive = ''Y''
      and a.c_customer_id = c.id
      and c.C_CUSRANK_ID = 2
      and C.isactive = ''Y''
      and c.IS_ONLINE = ''Y''
      and a.OPENCARDDATE between to_char(trunc(add_months(sysdate, -1), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -1)), ''YYYYMMDD'')
    group by substr(a.OPENCARDDATE, 1, 6),
             case
               when instr(c.name, ''总'') > 1
                 then decode(substr(c.name,1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
               else
                substr(c.name, 1, 3)
             end) b
    on a.name = b.name
union all
select nvl(a.months, b.months) months,
       nvl(a.name, b.name) name,
       nvl(a.bl, 0) bl,
       nvl(b.months, a.months) months1,
       nvl(b.name, a.name) name1,
       nvl(b.bl, 0) bl1
  from
 (select substr(a.OPENCARDDATE, 1, 6) months,
         case
           when instr(c.name, ''总'') > 1 then
            decode(substr(c.name, 1, instr(c.name, ''总'') - 1),''温州'',''新南昌'',substr(c.name, 1, instr(c.name, ''总'') - 1))
           else
            substr(c.name, 1, 3)
         end name,
         count(a.id) bl
    from c_client_vip a, c_customer c
   where a.isactive = ''Y''
     and a.c_customerup_id = c.id
     and c.C_CUSRANK_ID = 2
     and C.isactive = ''Y''
     and c.IS_ONLINE = ''Y''
     and a.OPENCARDDATE between to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -2)), ''YYYYMMDD'')
   group by substr(a.OPENCARDDATE, 1, 6),
            case
              when instr(c.name, ''总'') > 1
                then decode(substr(c.name, 1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
              else
               substr(c.name, 1, 3)
            end) a
  Full outer join
  (select substr(a.OPENCARDDATE, 1, 6) months,
          case
            when instr(c.name, ''总'') > 1
              then decode(substr(c.name, 1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
            else
             substr(c.name, 1, 3)
          end name,
          count(a.id) bl
     from c_client_vip a, c_customer c
    where a.isactive = ''Y''
      and a.c_customerup_id = c.id
      and c.C_CUSRANK_ID = 2
      and C.isactive = ''Y''
      and c.IS_ONLINE = ''Y''
      and a.OPENCARDDATE between to_char(trunc(add_months(sysdate, -1), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -1)), ''YYYYMMDD'')
    group by substr(a.OPENCARDDATE, 1, 6),
             case
               when instr(c.name, ''总'') > 1
                 then decode(substr(c.name,1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
               else
                substr(c.name, 1, 3)
             end) b
    on a.name = b.name)
    group by months,name
    order by months,bl desc', 'select months,name,sum(bl) bl,max(months1) months1,max(name1) name1,sum(bl1) bl1
from (
select nvl(a.months, b.months) months,
       nvl(a.name, b.name) name,
       nvl(a.bl, 0) bl,
       nvl(b.months, a.months) months1,
       nvl(b.name, a.name) name1,
       nvl(b.bl, 0) bl1
  from
 (select substr(a.OPENCARDDATE, 1, 6) months,
         case
           when instr(c.name, ''总'') > 1 then
            decode(substr(c.name, 1, instr(c.name, ''总'') - 1),''温州'',''新南昌'',substr(c.name, 1, instr(c.name, ''总'') - 1))
           else
            substr(c.name, 1, 3)
         end name,
         count(a.id) bl
    from c_client_vip a, c_customer c
   where a.isactive = ''Y''
     and a.c_customer_id = c.id
     and c.C_CUSRANK_ID = 2
     and C.isactive = ''Y''
     and c.IS_ONLINE = ''Y''
     and a.OPENCARDDATE between to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -2)), ''YYYYMMDD'')
   group by substr(a.OPENCARDDATE, 1, 6),
            case
              when instr(c.name, ''总'') > 1
                then decode(substr(c.name, 1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
              else
               substr(c.name, 1, 3)
            end) a
  Full outer join
  (select substr(a.OPENCARDDATE, 1, 6) months,
          case
            when instr(c.name, ''总'') > 1
              then decode(substr(c.name, 1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
            else
             substr(c.name, 1, 3)
          end name,
          count(a.id) bl
     from c_client_vip a, c_customer c
    where a.isactive = ''Y''
      and a.c_customer_id = c.id
      and c.C_CUSRANK_ID = 2
      and C.isactive = ''Y''
      and c.IS_ONLINE = ''Y''
      and a.OPENCARDDATE between to_char(trunc(add_months(sysdate, -1), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -1)), ''YYYYMMDD'')
    group by substr(a.OPENCARDDATE, 1, 6),
             case
               when instr(c.name, ''总'') > 1
                 then decode(substr(c.name,1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
               else
                substr(c.name, 1, 3)
             end) b
    on a.name = b.name
union all
select nvl(a.months, b.months) months,
       nvl(a.name, b.name) name,
       nvl(a.bl, 0) bl,
       nvl(b.months, a.months) months1,
       nvl(b.name, a.name) name1,
       nvl(b.bl, 0) bl1
  from
 (select substr(a.OPENCARDDATE, 1, 6) months,
         case
           when instr(c.name, ''总'') > 1 then
            decode(substr(c.name, 1, instr(c.name, ''总'') - 1),''温州'',''新南昌'',substr(c.name, 1, instr(c.name, ''总'') - 1))
           else
            substr(c.name, 1, 3)
         end name,
         count(a.id) bl
    from c_client_vip a, c_customer c
   where a.isactive = ''Y''
     and a.c_customerup_id = c.id
     and c.C_CUSRANK_ID = 2
     and C.isactive = ''Y''
     and c.IS_ONLINE = ''Y''
     and a.OPENCARDDATE between to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -2)), ''YYYYMMDD'')
   group by substr(a.OPENCARDDATE, 1, 6),
            case
              when instr(c.name, ''总'') > 1
                then decode(substr(c.name, 1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
              else
               substr(c.name, 1, 3)
            end) a
  Full outer join
  (select substr(a.OPENCARDDATE, 1, 6) months,
          case
            when instr(c.name, ''总'') > 1
              then decode(substr(c.name, 1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
            else
             substr(c.name, 1, 3)
          end name,
          count(a.id) bl
     from c_client_vip a, c_customer c
    where a.isactive = ''Y''
      and a.c_customerup_id = c.id
      and c.C_CUSRANK_ID = 2
      and C.isactive = ''Y''
      and c.IS_ONLINE = ''Y''
      and a.OPENCARDDATE between to_char(trunc(add_months(sysdate, -1), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -1)), ''YYYYMMDD'')
    group by substr(a.OPENCARDDATE, 1, 6),
             case
               when instr(c.name, ''总'') > 1
                 then decode(substr(c.name,1, instr(c.name, ''总'') - 1), ''温州'', ''新南昌'', substr(c.name, 1, instr(c.name, ''总'') - 1))
               else
                substr(c.name, 1, 3)
             end) b
    on a.name = b.name)
    group by months,name
    order by months,bl desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('30', '经销商开卡率', 'jxskkl', '2', '61', 'select a.name,
       a.billdate,
       round(a.newvip_count /(a.tot_count - (a.vip_count - a.newvip_count)) * 100,2) kkl,
       round(b.newvip_count /(b.tot_count - (b.vip_count - b.newvip_count)) * 100,2) kkl1
  from (select case
           when instr(sn.name, ''总'') > 1 then
            decode(substr(sn.name,1,instr(sn.name, ''总'') - 1),''温州'',''新南昌'',substr(sn.name,1,instr(sn.name, ''总'') - 1))
           else
            substr(sn.name, 1, 3)
      end name,
      substr(billdate, 0, 6) billdate,
      count(a.id) tot_count,
      count(case when a.c_vip_id is not null then a.id end) vip_count,
      count(case when v.opencarddate between to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -2)), ''YYYYMMDD'') then a.id end) newvip_count
 from m_retail a, sjfx_customer s, c_vip v,c_customer sn
where a.c_customer_id = s.id
  and a.c_vip_id = v.id(+)
  and a.billdate between to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -2)), ''YYYYMMDD'')
  and a.isactive = ''Y''
  and a.status = 2
  and sn.name <> ''测试经销商''
  and s.customer_id=sn.id
group by case
           when instr(sn.name, ''总'') > 1 then
            decode(substr(sn.name,1,instr(sn.name, ''总'') - 1),''温州'',''新南昌'',substr(sn.name,1,instr(sn.name, ''总'') - 1))
           else
            substr(sn.name, 1, 3)
         end,
         substr(billdate, 0, 6)
         ) a
  full outer join (
  select case
           when instr(sn.name, ''总'') > 1 then
            decode(substr(sn.name,1,instr(sn.name, ''总'') - 1),''温州'',''新南昌'',substr(sn.name,1,instr(sn.name, ''总'') - 1))
           else
            substr(sn.name, 1, 3)
      end name,
      substr(billdate, 0, 6) billdate,
      count(a.id) tot_count,
      count(case when a.c_vip_id is not null then a.id end) vip_count,
      count(case when v.opencarddate between to_char(trunc(add_months(sysdate, -1), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -1)), ''YYYYMMDD'') then a.id end) newvip_count
 from m_retail a, sjfx_customer s, c_vip v,c_customer sn
where a.c_customer_id = s.id
  and a.c_vip_id = v.id(+)
  and a.billdate between to_char(trunc(add_months(sysdate, -1), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -1)), ''YYYYMMDD'')
  and a.isactive = ''Y''
  and a.status = 2
  and sn.name <> ''测试经销商''
  and s.customer_id=sn.id
group by case
           when instr(sn.name, ''总'') > 1 then
            decode(substr(sn.name,1,instr(sn.name, ''总'') - 1),''温州'',''新南昌'',substr(sn.name,1,instr(sn.name, ''总'') - 1))
           else
            substr(sn.name, 1, 3)
         end,
         substr(billdate, 0, 6)
  ) b
    on a.name = b.name
 where round(a.newvip_count / (a.tot_count - (a.vip_count - a.newvip_count)) * 100,2) > 0
   and round(b.newvip_count / (b.tot_count - (b.vip_count - b.newvip_count)) * 100,2) > 0
 order by billdate,kkl desc
', 'select a.name,
       a.billdate,
       round(a.newvip_count /(a.tot_count - (a.vip_count - a.newvip_count)) * 100,2) kkl,
       round(b.newvip_count /(b.tot_count - (b.vip_count - b.newvip_count)) * 100,2) kkl1
  from (select case
           when instr(sn.name, ''总'') > 1 then
            decode(substr(sn.name,1,instr(sn.name, ''总'') - 1),''温州'',''新南昌'',substr(sn.name,1,instr(sn.name, ''总'') - 1))
           else
            substr(sn.name, 1, 3)
      end name,
      substr(billdate, 0, 6) billdate,
      count(a.id) tot_count,
      count(case when a.c_vip_id is not null then a.id end) vip_count,
      count(case when v.opencarddate between to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -2)), ''YYYYMMDD'') then a.id end) newvip_count
 from m_retail a, sjfx_customer s, c_vip v,c_customer sn
where a.c_customer_id = s.id
  and a.c_vip_id = v.id(+)
  and a.billdate between to_char(trunc(add_months(sysdate, -2), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -2)), ''YYYYMMDD'')
  and a.isactive = ''Y''
  and a.status = 2
  and sn.name <> ''测试经销商''
  and s.customer_id=sn.id
group by case
           when instr(sn.name, ''总'') > 1 then
            decode(substr(sn.name,1,instr(sn.name, ''总'') - 1),''温州'',''新南昌'',substr(sn.name,1,instr(sn.name, ''总'') - 1))
           else
            substr(sn.name, 1, 3)
         end,
         substr(billdate, 0, 6)
         ) a
  full outer join (
  select case
           when instr(sn.name, ''总'') > 1 then
            decode(substr(sn.name,1,instr(sn.name, ''总'') - 1),''温州'',''新南昌'',substr(sn.name,1,instr(sn.name, ''总'') - 1))
           else
            substr(sn.name, 1, 3)
      end name,
      substr(billdate, 0, 6) billdate,
      count(a.id) tot_count,
      count(case when a.c_vip_id is not null then a.id end) vip_count,
      count(case when v.opencarddate between to_char(trunc(add_months(sysdate, -1), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -1)), ''YYYYMMDD'') then a.id end) newvip_count
 from m_retail a, sjfx_customer s, c_vip v,c_customer sn
where a.c_customer_id = s.id
  and a.c_vip_id = v.id(+)
  and a.billdate between to_char(trunc(add_months(sysdate, -1), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(sysdate, -1)), ''YYYYMMDD'')
  and a.isactive = ''Y''
  and a.status = 2
  and sn.name <> ''测试经销商''
  and s.customer_id=sn.id
group by case
           when instr(sn.name, ''总'') > 1 then
            decode(substr(sn.name,1,instr(sn.name, ''总'') - 1),''温州'',''新南昌'',substr(sn.name,1,instr(sn.name, ''总'') - 1))
           else
            substr(sn.name, 1, 3)
         end,
         substr(billdate, 0, 6)
  ) b
    on a.name = b.name
 where round(a.newvip_count / (a.tot_count - (a.vip_count - a.newvip_count)) * 100,2) > 0
   and round(b.newvip_count / (b.tot_count - (b.vip_count - b.newvip_count)) * 100,2) > 0
 order by billdate,kkl desc
', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '券核销情况', 'qhxqk', '2', '64', 'select to_char(u.fdate, ''yyyyMM'') "月份",
       count(1) "领券",
       count(1) - sum(u.fid_sign) "核销"
  from dbcenter.vip_mall_coupon_user u
  left join iquest.mall_user_register r
    on u.fopen_id = r.fopen_id
  left join iquest.mall_code_manage c
    on c.fapp_id = r.fwx_id
   WHERE to_char(u.fdate, ''yyyyMM'')<>TO_char(sysdate,''YYYYMM'')
 group by to_char(u.fdate, ''yyyyMM'')
having count(1) - sum(u.fid_sign) > 0
 order by "月份" asc', 'select to_char(u.fdate, ''yyyyMM'') "月份",
       count(1) "领券",
       count(1) - sum(u.fid_sign) "核销"
  from dbcenter.vip_mall_coupon_user u
  left join iquest.mall_user_register r
    on u.fopen_id = r.fopen_id
  left join iquest.mall_code_manage c
    on c.fapp_id = r.fwx_id
    where to_char(u.fdate,''YYYYMM'')<>to_char(sysdate,''YYYYMM'')
 group by to_char(u.fdate, ''yyyyMM'')
having count(1) - sum(u.fid_sign) > 0
 order by "月份" desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '季节', 'jj', '13', '92', 'select ID,ATTRIBNAME NAME from m_dim@portal80 WHERE dimflag=''DIM9''', 'select ID,ATTRIBNAME NAME from m_dim@portal80 WHERE dimflag=''DIM9''', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '大数据看板-全国', 'dsjkb-qg', '5', '83', ' select substr(b.province, 0, 2) province,
 # case when length([province])>0 then b.city end city, #
 # case when length([city])>0 then b.district end district, #
        sum(a.qty) value,
        sum(a.tot_amt_actual) tot_amt_actual
   from ls_bill_m_retail a,
  (select * from table(f_store_table(''@fdd'')) where 1 =1) tx,
        bs_c_store  b
  where a.c_store_id = b.id
    and tx.c_store_id=a.c_store_id
    and months = @months
    and b.province is not null
    and b.city is not null
    and b.district is not null
  # and substr(b.province,1,2) = substr([province],1,2) #
 # and substr(b.city,1,2) = substr([city],1,2)  #
  group by substr(b.province, 0, 2)
 # ,case when length([province])>0 then b.city end #
 # ,case when length([city])>0 then b.district end # ', ' select substr(b.province, 0, 2) province,
 # case when length([province])>0 then b.city end city, #
 # case when length([city])>0 then b.district end district, #
        sum(a.qty) value,
        sum(a.tot_amt_actual) tot_amt_actual
   from ls_bill_m_retail a,
        bs_c_store  b
  where a.c_store_id = b.id
    and months = @months
    and b.province is not null
    and b.city is not null
    and b.district is not null
  # and substr(b.province,1,2) = substr([province],1,2) #
 # and substr(b.city,1,2) = substr([city],1,2)  #
  group by substr(b.province, 0, 2)
 # ,case when length([province])>0 then b.city end #
 # ,case when length([city])>0 then b.district end # ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '看板-零售额度-月', 'kb-lsed-y', '5', '84', ' select a.months, ''all'' type, sum(tot_amt_actual) Quota
   from ls_store_m_retail a, bs_c_store b,
       (select * from table(f_store_table(''@fdd'')) where 1 = 1) tx
  where 1 = 1
    and a.c_store_id = b.id
    and b.province is not null
    and b.city is not null
    and a.c_store_id=tx.c_store_id
  #  and substr(b.province, 0, length([province]))  #
  #  = [province] #
  #  and substr(b.city, 0, length([cityName]))  #
  #  = [city] #
    and months = @months
  group by a.months
 union all
 select a.months, ''vip'' type, sum(vip_pay) Quota
   from ls_store_m_retail a, bs_c_store b,
       (select * from table(f_store_table(''@fdd'')) where 1 = 1) tx
  where 1 = 1
    and a.c_store_id = b.id
    and b.province is not null
    and b.city is not null
    and a.c_store_id=tx.c_store_id
  #  and substr(b.province, 0, length([province]))  #
  #  = [province] #
  #  and substr(b.city, 0, length([city]))  #
  #  = [city] #
    and months = @months
  group by a.months', ' select a.months, ''all'' type, sum(tot_amt_actual) Quota
   from ls_store_m_retail a, bs_c_store b
  where 1 = 1
    and a.c_store_id = b.id
    and b.province is not null
    and b.city is not null
  #  and substr(b.province, 0, length([province]))  #
  #  = [province] #
  #  and substr(b.city, 0, length([city]))  #
  #  = [city] #
    and months = @months
  group by a.months
 union all
 select a.months, ''vip'' type, sum(vip_pay) Quota
   from ls_store_m_retail a, bs_c_store b
  where 1 = 1
    and a.c_store_id = b.id
    and b.province is not null
    and b.city is not null
  #  and substr(b.province, 0, length([province]))  #
  #  = [province] #
  #  and substr(b.city, 0, length([city]))  #
  #  = [city] #
    and months = @months
  group by a.months', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '看板-省份销售-月', 'kb-sfxs-y', '5', '86', ' select a.months, c.province province_name,
 # case when length([province])>0 then c.city end city_name, #
 # case when length([city])>0 then c.district end district_name, #
 sum(a.tot_amt_actual) tot_amt_actual
  from ls_store_m_retail a, bs_c_store c,
       (select * from table(f_store_table(''@fdd'')) where 1 = 1) tx
 where a.c_store_id = c.id
   and tx.c_store_id=a.c_store_id
   and c.province is not null
   and c.city is not null
 # and substr(c.PROVINCE,0,length([province])) #
 # =[province] #
 # and substr(c.city,0,length([city])) #
 # =[city] #
   and a.months = @months
 group by a.months , c.province
 # ,case when length([province])>0 then c.city end #
 # ,case when length([city])>0 then c.district end #
 order by tot_amt_actual desc', ' select a.months, c.province province_name,
 # case when length([province])>0 then c.city end city_name, #
 # case when length([city])>0 then c.district end district_name, #
 sum(a.tot_amt_actual) tot_amt_actual
  from ls_store_m_retail a, bs_c_store c
 where a.c_store_id = c.id
   and c.province is not null
   and c.city is not null
 # and substr(c.PROVINCE,0,length([province])) #
 # =[province] #
 # and substr(c.city,0,length([city])) #
 # =[city] #
   and a.months = @months
 group by a.months , c.province
 # ,case when length([province])>0 then c.city end #
 # ,case when length([city])>0 then c.district end #
 order by tot_amt_actual desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '单笔金额', 'dbje', '15', '113', 'select case
         when je.tot_amt_actual < 100 then ''小于100$''
         when je.tot_amt_actual >= 100 and je.tot_amt_actual < 150 then ''100$-150$''
         when je.tot_amt_actual >= 150 and je.tot_amt_actual < 200 then ''150$-200$''
         when je.tot_amt_actual >= 200 and je.tot_amt_actual < 300 then ''200$-300$''
         when je.tot_amt_actual >= 300 then ''大于300$''
       end name,
       count(1) qty
  from (select a.docno, sum(a.tot_amt_actual) tot_amt_actual
          from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
         where 1 = 1
           and a.c_store_id=tx.c_store_id
   and a.m_product_id=b.id
         # and a.billdate between [beginDate] #
         # and [endDate] #
         # and a.billdate <= [jzDate] #
         # and a.billdate between [qsDate] and to_char(sysdate - 1, ''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
           and a.status = 2
           and a.c_vip_id is not null
           and a.isactive = ''Y''
         group by a.docno) je
 where je.tot_amt_actual > 0
 group by case
            when je.tot_amt_actual < 100 then ''小于100$''
            when je.tot_amt_actual >= 100 and je.tot_amt_actual < 150 then ''100$-150$''
            when je.tot_amt_actual >= 150 and je.tot_amt_actual < 200 then ''150$-200$''
            when je.tot_amt_actual >= 200 and je.tot_amt_actual < 300 then ''200$-300$''
            when je.tot_amt_actual >= 300 then ''大于300$''
          end', 'select case
         when je.tot_amt_actual < 100 then ''小于100$''
         when je.tot_amt_actual >= 100 and je.tot_amt_actual < 150 then ''100$-150$''
         when je.tot_amt_actual >= 150 and je.tot_amt_actual < 200 then ''150$-200$''
         when je.tot_amt_actual >= 200 and je.tot_amt_actual < 300 then ''200$-300$''
         when je.tot_amt_actual >= 300 then ''大于300$''
       end name,
       count(1) qty
  from (select a.docno, sum(a.tot_amt_actual) tot_amt_actual
          from ls_bill_d_retail a,BIFX_M_PRODUCT b
         where 1 = 1
   and a.m_product_id=b.id
         # and a.billdate between [beginDate] #
         # and [endDate] #
         # and a.billdate <= [jzDate] #
         # and a.billdate between [qsDate] and to_char(sysdate - 1, ''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
           and a.status = 2
           and a.c_vip_id is not null
           and a.isactive = ''Y''
         group by a.docno) je
 where je.tot_amt_actual > 0
 group by case
            when je.tot_amt_actual < 100 then ''小于100$''
            when je.tot_amt_actual >= 100 and je.tot_amt_actual < 150 then ''100$-150$''
            when je.tot_amt_actual >= 150 and je.tot_amt_actual < 200 then ''150$-200$''
            when je.tot_amt_actual >= 200 and je.tot_amt_actual < 300 then ''200$-300$''
            when je.tot_amt_actual >= 300 then ''大于300$''
          end', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '看板-店铺排名-月', 'kb-dppm-y', '5', '85', 'select x.rns,x.name,x.c_store_id,x.qty,x.tot_amt_actual,x.avg_price, x.indicator_over_rate,nvl(days_avg,0)days_avg
  from (
  select t.c_store_id, c.name,t.month,row_number() over(partition by t.month  order by sum(t.tot_amt_actual) desc) rns,
               sum(t.qty)qty, sum(t.tot_amt_actual)tot_amt_actual,round(decode(sum(t.qty),0,0,sum(t.tot_amt_actual)/sum(t.qty)),2) avg_price,
               nvl(max(t.indicator_over_rate),0) indicator_over_rate
          from (select  * from ls_store_d_retail s, view_daylist d  where d.billdate = s.billdate and d.month = @months
          ) t, view_bs_c_store c ,
       (select * from table(f_store_table(''@fdd'')) where 1 = 1) tx
         where t.c_store_id = c.id
           and tx.c_store_id=t.c_store_id
           and c.province is not null
           and c.city is not null
        #   and substr(c.PROVINCE,0,length([province])) #
        #   =[province] #
        #   and substr(c.city,0,length([city])) #
        #   =[city] #
         group by t.c_store_id, c.name,t.month
         )x,
       (select m.c_store_id,sum(m.tot_amt_actual)tot_amt_actual,
               round(sum(m.tot_amt_actual)/substr(max(billdate),7),2) days_avg
          from (select s.*
                  from ls_store_d_retail s, view_daylist d,bs_c_store c
                 where d.billdate = s.billdate
                   and c.id=s.c_store_id
                   and c.province is not null
                   and c.city is not null
        #   and substr(c.PROVINCE,0,length([province])) #
        #   =[province] #
        #   and substr(c.city,0,length([city])) #
        #   =[city] #
                   and d.month =@months
                   ) m
         group by m.c_store_id)y
where x.c_store_id = y.c_store_id(+)
order by tot_amt_actual desc
', 'select x.rns,x.name,x.c_store_id,x.qty,x.tot_amt_actual,x.avg_price, x.indicator_over_rate,nvl(days_avg,0)days_avg
  from (
  select t.c_store_id, c.name,t.month,row_number() over(partition by t.month  order by sum(t.tot_amt_actual) desc) rns,
               sum(t.qty)qty, sum(t.tot_amt_actual)tot_amt_actual,round(decode(sum(t.qty),0,0,sum(t.tot_amt_actual)/sum(t.qty)),2) avg_price,
               nvl(max(t.indicator_over_rate),0) indicator_over_rate
          from (select  * from ls_store_d_retail s, view_daylist d  where d.billdate = s.billdate and d.month = @months
          ) t, view_bs_c_store c
         where t.c_store_id = c.id
           and c.province is not null
           and c.city is not null
        #   and substr(c.PROVINCE,0,length([province])) #
        #   =[province] #
        #   and substr(c.city,0,length([city])) #
        #   =[city] #
         group by t.c_store_id, c.name,t.month
         )x,
       (select m.c_store_id,sum(m.tot_amt_actual)tot_amt_actual,
               round(sum(m.tot_amt_actual)/substr(max(billdate),7),2) days_avg
          from (select s.*
                  from ls_store_d_retail s, view_daylist d,bs_c_store c
                 where d.billdate = s.billdate
                   and c.id=s.c_store_id
           and c.province is not null
           and c.city is not null
        #   and substr(c.PROVINCE,0,length([province])) #
        #   =[province] #
        #   and substr(c.city,0,length([city])) #
        #   =[city] #
                   and d.month =@months
                   ) m
         group by m.c_store_id)y
where x.c_store_id = y.c_store_id(+)
order by tot_amt_actual desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '看板-年同比-销-数量', 'kb-ntb-x-sl', '5', '87', 'select substr(a.billdate,0,4) year,substr(a.billdate,5,4) monthDay,a.billdate,sum(tot_amt_actual) tot_amt_actual
  from ls_store_d_retail a,bs_c_store c,
       (select * from table(f_store_table(''@fdd'')) where 1 = 1) tx
 where a.c_store_id=c.id
   and a.c_store_id=tx.c_store_id
 # and substr(c.province,0,length([province])) #
 # =[province] #
 # and substr(c.city,0,length([city])) #
 # =[city] #
   and a.billdate between to_char(trunc(to_date(@months,''YYYYMM''),''mm''),''YYYYMMDD'') and to_char(last_day(to_date(@months,''YYYYMM'')), ''YYYYMMDD'')
 group by a.billdate
union all
select substr(a.billdate,0,4) year,substr(a.billdate,5,4) monthDay,a.billdate,sum(tot_amt_actual) tot_amt_actual
  from ls_store_d_retail a,bs_c_store c,
       (select * from table(f_store_table(''@fdd'')) where 1 = 1) tx
 where a.c_store_id=c.id
   and a.c_store_id=tx.c_store_id
 # and substr(c.province,0,length([province])) #
 # =[province] #
 # and substr(c.city,0,length([city])) #
 # =[city] #
   and a.billdate between to_char(trunc(add_months(to_date(@months,''YYYYMM''),-12), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(to_date(@months,''YYYYMM''),-12)), ''YYYYMMDD'')
  group by a.billdate
  order by billdate', 'select substr(a.billdate,0,4) year,substr(a.billdate,5,4) monthDay,a.billdate,sum(tot_amt_actual) tot_amt_actual
  from ls_store_d_retail a,bs_c_store c
 where a.c_store_id=c.id
 # and substr(c.province,0,length([province])) #
 # =[province] #
 # and substr(c.city,0,length([city])) #
 # =[city] #
   and a.billdate between to_char(trunc(to_date(@months,''YYYYMM''),''mm''),''YYYYMMDD'') and to_char(last_day(to_date(@months,''YYYYMM'')), ''YYYYMMDD'')
 group by a.billdate
union all
select substr(a.billdate,0,4) year,substr(a.billdate,5,4) monthDay,a.billdate,sum(tot_amt_actual) tot_amt_actual
  from ls_store_d_retail a,bs_c_store c
 where a.c_store_id=c.id
 # and substr(c.province,0,length([province])) #
 # =[province] #
 # and substr(c.city,0,length([city])) #
 # =[city] #
   and a.billdate between to_char(trunc(add_months(to_date(@months,''YYYYMM''),-12), ''mm''), ''YYYYMMDD'') and to_char(last_day(add_months(to_date(@months,''YYYYMM''),-12)), ''YYYYMMDD'')
  group by a.billdate
  order by billdate', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '总经销商', 'zjxs', '13', '89', 'SELECT S.ID, S.NAME,
(case when instr(s.name,''总'')>0 then substr(s.name,0,instr(s.name,''总'')-1) else substr(s.name,0,3) end) name1
  FROM C_CUSTOMER@PORTAL80 S,
  (select * from table(f_store_table(''@fdd'')) where 1 =1) tx
 WHERE s.id=tx.c_customer_id
   and S.C_CUSRANK_ID = 2
   AND S.ISACTIVE = ''Y''
   and s.IS_ONLINE=''Y''
   AND s.name NOT LIKE ''%旧%''
   AND s.name NOT LIKE ''%老%''
   AND s.id NOT IN (15177,15184,15183,177,184,194,197,196,205,212,6075)
   AND s.description NOT LIKE ''%已停用%''
group by s.id,s.name,
case when instr(s.name,''总'')>0 then substr(s.name,0,instr(s.name,''总'')-1) else substr(s.name,0,3) end
order by name1,name', 'SELECT S.ID, S.NAME,
(case when instr(s.name,''总'')>0 then substr(s.name,0,instr(s.name,''总'')-1) else substr(s.name,0,3) end) name1
  FROM C_CUSTOMER@PORTAL80 S
 WHERE S.C_CUSRANK_ID = 2
   AND S.ISACTIVE = ''Y''
   and s.IS_ONLINE=''Y''
   AND s.name NOT LIKE ''%旧%''
   AND s.name NOT LIKE ''%老%''
   AND s.id NOT IN (15177,15184,15183,177,184,194,197,196,205,212,6075)
   AND s.description NOT LIKE ''%已停用%''
group by s.id,s.name,
case when instr(s.name,''总'')>0 then substr(s.name,0,instr(s.name,''总'')-1) else substr(s.name,0,3) end
order by name1,name', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '看板-会员复购-月', 'kb-hyfg-y', '5', '88', ' select b.province,
 # case when length([province])>0 then b.city end city, #
 # case when length([city])>0 then b.district end district, #
 sum(wa.qty) wa_qty,sum(wb.qty) wb_qty,
 round(sum(wa.qty)/sum(wb.qty)*10000)/100 fgl
  from
(select a.c_customer_id,a.c_store_id,sum(qty) qty
  from ls_bill_d_retail a,is_vip_retail b
 where a.c_vip_id=b.c_vip_id
   and a.c_customer_id=b.c_customer_id
   and a.c_store_id=b.c_store_id
   and b.year_months=@months
   and a.c_vip_id is not null
   and a.billdate between
       to_char(trunc(to_date(@months, ''YYYYMM''), ''mm''), ''YYYYMMDD'') and
       to_char(last_day(to_date(@months, ''YYYYMM'')), ''YYYYMMDD'')
group by a.c_customer_id,a.c_store_id) wa,
(select a.c_customer_id,a.c_store_id,sum(qty) qty
  from ls_bill_d_retail a
 where a.c_vip_id is not null
   and a.billdate between
       to_char(trunc(to_date(@months, ''YYYYMM''), ''mm''), ''YYYYMMDD'') and
       to_char(last_day(to_date(@months, ''YYYYMM'')), ''YYYYMMDD'')
 group by a.c_customer_id,a.c_store_id) wb,
bs_c_store b,
(select * from table(f_store_table(''@fdd'')) where 1 =1) tx
where wa.c_customer_id=wb.c_customer_id
and wa.c_store_id=wb.c_store_id
and wa.c_store_id=b.id
and tx.c_store_id=wa.c_store_id
and b.province is not null
and b.city is not null
and b.district is not null
  # and substr(b.province,1,2) = substr([province],1,2) #
 # and substr(b.city,1,2) = substr([city],1,2)  #
group by b.province
 # ,case when length([province])>0 then b.city end #
 # ,case when length([city])>0 then b.district end #
order by fgl desc', ' select b.province,
 # case when length([province])>0 then b.city end city, #
 # case when length([city])>0 then b.district end district, #
 sum(wa.qty) wa_qty,sum(wb.qty) wb_qty,
 round(sum(wa.qty)/sum(wb.qty)*10000)/100 fgl
  from
(select a.c_customer_id,a.c_store_id,sum(qty) qty
  from ls_bill_d_retail a,is_vip_retail b
 where a.c_vip_id=b.c_vip_id
   and a.c_customer_id=b.c_customer_id
   and a.c_store_id=b.c_store_id
   and b.year_months=@months
   and a.c_vip_id is not null
   and a.billdate between
       to_char(trunc(to_date(@months, ''YYYYMM''), ''mm''), ''YYYYMMDD'') and
       to_char(last_day(to_date(@months, ''YYYYMM'')), ''YYYYMMDD'')
group by a.c_customer_id,a.c_store_id) wa,
(select a.c_customer_id,a.c_store_id,sum(qty) qty
  from ls_bill_d_retail a
 where a.c_vip_id is not null
   and a.billdate between
       to_char(trunc(to_date(@months, ''YYYYMM''), ''mm''), ''YYYYMMDD'') and
       to_char(last_day(to_date(@months, ''YYYYMM'')), ''YYYYMMDD'')
 group by a.c_customer_id,a.c_store_id) wb,
bs_c_store b
where wa.c_customer_id=wb.c_customer_id
and wa.c_store_id=wb.c_store_id
and wa.c_store_id=b.id
and b.province is not null
and b.city is not null
and b.district is not null
  # and substr(b.province,1,2) = substr([province],1,2) #
 # and substr(b.city,1,2) = substr([city],1,2)  #
group by b.province
 # ,case when length([province])>0 then b.city end #
 # ,case when length([city])>0 then b.district end #
order by fgl desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '单笔金额明细', 'dbjemx', '15', '106', 'select billdate,docno,c_store_name,tot_amt_actual,single_amount
 from (
select a.billdate,a.docno,a.c_store_name
  # ,case when [singleAmount] is not null then sum(a.tot_amt_actual) end tot_amt_actual #
  # ,case when [singleAmount] is not null then sum(a.tot_amt_actual) end single_amount#
   from ls_bill_d_retail a,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
  where 1=1
    and a.c_store_id=tx.c_store_id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 # and a.billdate <= [jzDate] #
 # and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
 # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''
 group by a.billdate,a.docno,a.c_store_name)
where
  #    ([singleAmount] = to_char(''100-150'') and tot_amt_actual >= 100 and tot_amt_actual < 150) #
  # or ([singleAmount] = to_char(''150-200'') and tot_amt_actual >= 150 and tot_amt_actual < 200) #
  # or ([singleAmount] = to_char(''200-300'') and tot_amt_actual >= 200 and tot_amt_actual < 300) #
  # or ([singleAmount] = to_char(''大于300'') and tot_amt_actual > 300) #
  # or ([singleAmount] = to_char(''小于100'') and tot_amt_actual < 100) # ', 'select billdate,docno,c_store_name,tot_amt_actual,single_amount
 from (
select a.billdate,a.docno,a.c_store_name
  # ,case when [singleAmount] is not null then sum(a.tot_amt_actual) end tot_amt_actual #
  # ,case when [singleAmount] is not null then sum(a.tot_amt_actual) end single_amount #
   from ls_bill_d_retail a
  where 1=1
 # and a.billdate between [beginDate] #
 # and [endDate] #
 # and a.billdate <= [jzDate] #
 # and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
 # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''
 group by a.billdate,a.docno,a.c_store_name)
where
  #    ([singleAmount] = to_char(''100-150'') and tot_amt_actual >= 100 and tot_amt_actual < 150) #
  # or ([singleAmount] = to_char(''150-200'') and tot_amt_actual >= 150 and tot_amt_actual < 200) #
  # or ([singleAmount] = to_char(''200-300'') and tot_amt_actual >= 200 and tot_amt_actual < 300) #
  # or ([singleAmount] = to_char(''大于300'') and tot_amt_actual > 300) #
  # or ([singleAmount] = to_char(''小于100'') and tot_amt_actual < 100) # ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员积分明细', 'hyjfmx', '15', '107', 'select cc.cardno,cc.vipname,cc.mobil,cc.birthday,cc.ttl_integral membership_points
  from (select distinct a.c_vip_id
    from ls_bill_d_retail a, BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
   where 1 = 1
     and a.c_store_id=tx.c_store_id
     and a.m_product_id = b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, '''') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
     and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
     and a.c_vip_id is not null) a
  left join bi_c_vip cc
    on a.c_vip_id = cc.id
  left join bi_c_store c
    on cc.c_store_id=c.id
    where 1=1
   # and ([membershipPoints]= to_char(''小于1K'') and cc.ttl_integral<1000) #
   # or ([membershipPoints]= to_char(''1K-2K'') and cc.ttl_integral >= 1000 and cc.ttl_integral<2000) #
   # or ([membershipPoints]= to_char(''2K-3K'') and cc.ttl_integral >= 2000 and cc.ttl_integral<3000) #
   # or ([membershipPoints]= to_char(''大于3K'') and cc.ttl_integral >= 3000) # ', 'select cc.cardno,cc.vipname,cc.mobil,cc.birthday,cc.ttl_integral membership_points
  from (select distinct a.c_vip_id
    from ls_bill_d_retail a, BIFX_M_PRODUCT b
   where 1 = 1
     and a.m_product_id = b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, '''') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
     and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
     and a.c_vip_id is not null) a
  left join bi_c_vip cc
    on a.c_vip_id = cc.id
  left join bi_c_store c
    on cc.c_store_id=c.id
    where 1=1
   # and ([membershipPoints]= to_char(''小于1K'') and cc.ttl_integral<1000) #
   # or ([membershipPoints]= to_char(''1K-2K'') and cc.ttl_integral >= 1000 and cc.ttl_integral<2000) #
   # or ([membershipPoints]= to_char(''2K-3K'') and cc.ttl_integral >= 2000 and cc.ttl_integral<3000) #
   # or ([membershipPoints]= to_char(''大于3K'') and cc.ttl_integral >= 3000) # ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '零售分析-详细', 'lsfx-xx', '14', '101', ' select a.billdate,a.docno,a.c_store_name,a.m_product_name,a.tot_amt_actual
  # ,case when [timeInterval] is not null then to_char(a.creationdate,''HH24:mi:ss'') end time_interval #
  # ,case when [productStyle] is not null then c.attribname end product_style #
  # ,case when [vipProportion] is not null then nvl(v.mobil,v.cardno) end vip_proportion #
  # ,case when [codeSegment] is not null then a.sizes end code_segment #
  # ,case when [discountRate] is not null then to_char(a.discount,''0.99'') end discount_rate #
  # ,case when [commodityCategory] is not null then m6.attribname end commodity_category #
  from ls_bill_d_retail a left join BIFX_M_PRODUCT b on a.m_product_id = b.id
       left join m_dim@portal80 c on a.m_dim8_id = c.id
       left join c_vip@portal80 v on a.c_vip_id = v.id
       left join m_dim@portal80 m6 on b.m_dim6_id = m6.id
       right join (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx on a.c_store_id=tx.c_store_id
  where 1=1
  # and a.billdate between [beginDate] #
  # and [endDate] #
  # and a.billdate <= [jzDate] #
  # and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
  # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
  # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
  # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
  # and to_char(a.creationdate,''HH24'') in ([timeInterval]) #
  # and c.attribname = [productStyle] #
  # and m6.attribname= [commodityCategory] #
  # and ([vipProportion]=to_char(''是'') and a.c_vip_id is not null) #
  # or ([vipProportion]=to_char(''否'') and a.c_vip_id is null) #
  # and a.sizes = [codeSegment] #
  # and (([discountRate]= to_char(''小于3折'') and a.discount<=0.3) #
  # or ([discountRate]= to_char(''3-5折'') and a.discount > 0.3 and a.discount <= 0.5) #
  # or ([discountRate]= to_char(''5-7折'') and a.discount > 0.5 and a.discount <= 0.7) #
  # or ([discountRate]= to_char(''大于7折'') and a.discount > 0.7)) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''', ' select a.billdate,a.docno,a.c_store_name,a.m_product_name,a.tot_amt_actual
  # ,case when [timeInterval] is not null then to_char(a.creationdate,''HH24:mi:ss'') end time_interval #
  # ,case when [productStyle] is not null then c.attribname end product_style #
  # ,case when [vipProportion] is not null then nvl(v.mobil,v.cardno) end vip_proportion #
  # ,case when [codeSegment] is not null then a.sizes end code_segment #
  # ,case when [discountRate] is not null then to_char(a.discount,''0.99'') end discount_rate #
  # ,case when [commodityCategory] is not null then  c.attribname end commodity_category #
  from   ls_bill_d_retail a left join BIFX_M_PRODUCT b on a.m_product_id = b.id
       left join m_dim@portal80 c on a.m_dim8_id = c.id
       left join c_vip@portal80 v on a.c_vip_id = v.id
       left join m_dim@portal80 m6 on b.m_dim6_id = m6.id
  where 1=1
  # and a.billdate between [beginDate] #
  # and [endDate] #
  # and a.billdate <= [jzDate] #
  # and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
  # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
  # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
  # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
  # and m6.attribname= [commodityCategory] #
  # and to_char(a.creationdate,''HH24'') in ([timeInterval]) #
  # and c.attribname = [productStyle] #
  # and m6.attribname= [commodityCategory] #
  # and ([vipProportion]=to_char(''是'') and a.c_vip_id is not null) #
  # or ([vipProportion]=to_char(''否'') and a.c_vip_id is null) #
  # and a.sizes = [codeSegment] #
  # and (([discountRate]= to_char(''小于3折'') and a.discount<=0.3) #
  # or ([discountRate]= to_char(''3-5折'') and a.discount > 0.3 and a.discount <= 0.5) #
  # or ([discountRate]= to_char(''5-7折'') and a.discount > 0.5 and a.discount <= 0.7) #
  # or ([discountRate]= to_char(''大于7折'') and a.discount > 0.7)) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '时段分析', 'sdfx', '14', '102', ' select case
          when to_char(a.creationdate, ''HH24'') in (''00'', ''01'') then ''00-01''
          when to_char(a.creationdate, ''HH24'') in (''02'', ''03'',''04'',''05'') then ''02-05''
          when to_char(a.creationdate, ''HH24'') in (''06'', ''07'',''08'') then ''06-08''
          when to_char(a.creationdate, ''HH24'') in (''09'', ''10'') then ''09-10''
          when to_char(a.creationdate, ''HH24'') in (''11'', ''12'') then ''11-12''
          when to_char(a.creationdate, ''HH24'') in (''13'', ''14'') then ''13-14''
          when to_char(a.creationdate, ''HH24'') in (''15'', ''16'') then ''15-16''
          when to_char(a.creationdate, ''HH24'') in (''17'', ''18'') then ''17-18''
          when to_char(a.creationdate, ''HH24'') in (''19'', ''20'') then ''19-20''
          when to_char(a.creationdate, ''HH24'') in (''21'',''22'',''23'',''24'') then ''21-24''
        end name,
        count(1) qty,
        sum(a.tot_amt_actual) tot_amt_actual
   from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
  where 1=1
    and a.m_product_id=b.id
    and a.c_store_id=tx.c_store_id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
    and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
    and a.isactive = ''Y''
  group by case
          when to_char(a.creationdate, ''HH24'') in (''00'', ''01'') then ''00-01''
          when to_char(a.creationdate, ''HH24'') in (''02'', ''03'',''04'',''05'') then ''02-05''
          when to_char(a.creationdate, ''HH24'') in (''06'', ''07'',''08'') then ''06-08''
          when to_char(a.creationdate, ''HH24'') in (''09'', ''10'') then ''09-10''
          when to_char(a.creationdate, ''HH24'') in (''11'', ''12'') then ''11-12''
          when to_char(a.creationdate, ''HH24'') in (''13'', ''14'') then ''13-14''
          when to_char(a.creationdate, ''HH24'') in (''15'', ''16'') then ''15-16''
          when to_char(a.creationdate, ''HH24'') in (''17'', ''18'') then ''17-18''
          when to_char(a.creationdate, ''HH24'') in (''19'', ''20'') then ''19-20''
          when to_char(a.creationdate, ''HH24'') in (''21'',''22'',''23'',''24'') then ''21-24''
        end
  order by name', ' select case
          when to_char(a.creationdate, ''HH24'') in (''00'', ''01'') then ''00-01''
          when to_char(a.creationdate, ''HH24'') in (''02'', ''03'',''04'',''05'') then ''02-05''
          when to_char(a.creationdate, ''HH24'') in (''06'', ''07'',''08'') then ''06-08''
          when to_char(a.creationdate, ''HH24'') in (''09'', ''10'') then ''09-10''
          when to_char(a.creationdate, ''HH24'') in (''11'', ''12'') then ''11-12''
          when to_char(a.creationdate, ''HH24'') in (''13'', ''14'') then ''13-14''
          when to_char(a.creationdate, ''HH24'') in (''15'', ''16'') then ''15-16''
          when to_char(a.creationdate, ''HH24'') in (''17'', ''18'') then ''17-18''
          when to_char(a.creationdate, ''HH24'') in (''19'', ''20'') then ''19-20''
          when to_char(a.creationdate, ''HH24'') in (''21'',''22'',''23'',''24'') then ''21-24''
        end name,
        count(1) qty,
        sum(a.tot_amt_actual) tot_amt_actual
   from ls_bill_d_retail a,BIFX_M_PRODUCT b
  where 1=1
    and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
    and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
    and a.isactive = ''Y''
  group by case
          when to_char(a.creationdate, ''HH24'') in (''00'', ''01'') then ''00-01''
          when to_char(a.creationdate, ''HH24'') in (''02'', ''03'',''04'',''05'') then ''02-05''
          when to_char(a.creationdate, ''HH24'') in (''06'', ''07'',''08'') then ''06-08''
          when to_char(a.creationdate, ''HH24'') in (''09'', ''10'') then ''09-10''
          when to_char(a.creationdate, ''HH24'') in (''11'', ''12'') then ''11-12''
          when to_char(a.creationdate, ''HH24'') in (''13'', ''14'') then ''13-14''
          when to_char(a.creationdate, ''HH24'') in (''15'', ''16'') then ''15-16''
          when to_char(a.creationdate, ''HH24'') in (''17'', ''18'') then ''17-18''
          when to_char(a.creationdate, ''HH24'') in (''19'', ''20'') then ''19-20''
          when to_char(a.creationdate, ''HH24'') in (''21'',''22'',''23'',''24'') then ''21-24''
        end
  order by name', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '风格分析', 'fgfx', '14', '103', 'select c.attribname name, sum(1) qty
  from ls_bill_d_retail a,BIFX_M_PRODUCT b,m_dim@portal80 c,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where 1=1
   and a.c_store_id=tx.c_store_id
   and a.m_dim8_id=c.id
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''
   and a.m_dim8_id is not null
 group by c.attribname', 'select c.attribname name, sum(1) qty
  from ls_bill_d_retail a,BIFX_M_PRODUCT b,m_dim@portal80 c
 where 1=1
   and a.m_dim8_id=c.id
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''
   and a.m_dim8_id is not null
 group by c.attribname', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员占比', 'hyzb', '14', '104', 'select case  when c_vip_id is null then ''否'' else ''是'' end name,
       count(a.id) qty
  from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where 1=1
   and a.m_product_id=b.id
   and a.c_store_id=tx.c_store_id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''
 group by case when c_vip_id is null then ''否''  else  ''是''  end', 'select case  when c_vip_id is null then ''否'' else ''是'' end name,
       count(a.id) qty
  from ls_bill_d_retail a,BIFX_M_PRODUCT b
 where 1=1
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''
 group by case when c_vip_id is null then ''否''  else  ''是''  end', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '码段分析', 'mdfx', '14', '105', ' select decode(a.sizes,''00'',''包包'',a.sizes||''码'') name,
  sum(tot_amt_actual) qty
  from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where 1=1
   and a.m_product_id=b.id
   and a.c_store_id=tx.c_store_id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
	 and b.name not like ''ZS%''
   and a.isactive = ''Y''
 group by decode(a.sizes,''00'',''包包'',a.sizes||''码'')', ' select decode(a.sizes,''00'',''包包'',a.sizes||''码'') name,
  sum(tot_amt_actual) qty
  from ls_bill_d_retail a,BIFX_M_PRODUCT b
 where 1=1
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate > 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
	 and b.name not like ''ZS%''
   and a.isactive = ''Y''
 group by decode(a.sizes,''00'',''包包'',a.sizes||''码'')', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', 'BI店仓查询结果集', 'bidccxjgj', '13', '90', 'select aaa.*
from table(f_store_table(''@fdd'')) tx ,(
select customer.id customer_id, customer.name customer_name, c.id store_id, c.name store_name
  from (SELECT S.ID,
               S.NAME,
               substr(s.name, 0, instr(s.name, ''总'') - 1) name1
          FROM C_CUSTOMER@PORTAL80 S
         WHERE S.C_CUSRANK_ID = 2
           AND S.ISACTIVE = ''Y''
           and s.IS_ONLINE = ''Y''
           AND s.name NOT LIKE ''%旧%''
           AND s.name NOT LIKE ''%老%''
           AND S.NAME LIKE ''%总经销商''
           AND s.id NOT IN
               (15177, 15184, 15183, 177, 184, 194, 197, 196, 205, 212, 6075)
           AND s.description NOT LIKE ''%已停用%''
         order by name1, name) customer ,
       (select id,name,c_customer_id from c_store@PORTAL80 a where a.isactive=''Y'' and a.C_STOREKIND_id=1 and a.name not like ''%测试%''  ) c
 where customer.id = c.c_customer_id
UNION
select customer.id, customer.name, c.id, c.name
  from (SELECT S.ID,
               S.NAME,
               substr(s.name, 0, instr(s.name, ''总'') - 1) name1
          FROM C_CUSTOMER@PORTAL80 S
         WHERE S.C_CUSRANK_ID = 2
           AND S.ISACTIVE = ''Y''
           and s.IS_ONLINE = ''Y''
           AND s.name NOT LIKE ''%旧%''
           AND s.name NOT LIKE ''%老%''
           AND S.NAME LIKE ''%总经销商''
           AND s.id NOT IN
               (15177, 15184, 15183, 177, 184, 194, 197, 196, 205, 212, 6075)
           AND s.description NOT LIKE ''%已停用%''
         order by name1, name) customer ,
       (select id,name,c_customerup_id from c_store@PORTAL80 a where a.isactive=''Y'' and a.C_STOREKIND_id=1 and a.name not like ''%测试%''  ) c
 where customer.id = c.c_customerup_id) aaa
 where tx.c_store_id=aaa.store_id
order by customer_id
', 'select customer.id customer_id, customer.name customer_name, c.id store_id, c.name store_name
  from (SELECT S.ID,
               S.NAME,
               substr(s.name, 0, instr(s.name, ''总'') - 1) name1
          FROM C_CUSTOMER@PORTAL80 S
         WHERE S.C_CUSRANK_ID = 2
           AND S.ISACTIVE = ''Y''
           and s.IS_ONLINE = ''Y''
           AND s.name NOT LIKE ''%旧%''
           AND s.name NOT LIKE ''%老%''
           AND S.NAME LIKE ''%总经销商''
           AND s.id NOT IN
               (15177, 15184, 15183, 177, 184, 194, 197, 196, 205, 212, 6075)
           AND s.description NOT LIKE ''%已停用%''
         order by name1, name) customer ,
       (select id,name,c_customer_id from c_store@PORTAL80 a where a.isactive=''Y'' and a.C_STOREKIND_id=1 and a.name not like ''%测试%''  ) c
 where customer.id = c.c_customer_id
UNION
select customer.id, customer.name, c.id, c.name
  from (SELECT S.ID,
               S.NAME,
               substr(s.name, 0, instr(s.name, ''总'') - 1) name1
          FROM C_CUSTOMER@PORTAL80 S
         WHERE S.C_CUSRANK_ID = 2
           AND S.ISACTIVE = ''Y''
           and s.IS_ONLINE = ''Y''
           AND s.name NOT LIKE ''%旧%''
           AND s.name NOT LIKE ''%老%''
           AND S.NAME LIKE ''%总经销商''
           AND s.id NOT IN
               (15177, 15184, 15183, 177, 184, 194, 197, 196, 205, 212, 6075)
           AND s.description NOT LIKE ''%已停用%''
         order by name1, name) customer ,
       (select id,name,c_customerup_id from c_store@PORTAL80 a where a.isactive=''Y'' and a.C_STOREKIND_id=1 and a.name not like ''%测试%''  ) c
 where customer.id = c.c_customerup_id
order by customer_id', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('30', '年份', 'nf', '13', '91', 'select ID,ATTRIBNAME from m_dim WHERE dimflag=''DIM2''', 'select ID,ATTRIBNAME from m_dim WHERE dimflag=''DIM2''', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员购买次数明细', 'hygmcsmx', '16', '108', 'select c.name store_name,v.cardno,v.vipname,v.mobil,v.birthday
  # ,case when [frequency] is not null then aa.qty end frequency #
  from (
select c_vip_id, count(1) qty
  from (select c_vip_id, docno
          from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
         where 1 = 1
   and a.c_store_id=tx.c_store_id
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 # and a.billdate <= [jzDate] #
 # and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
 # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
   and a.status = 2
   and a.isactive = ''Y''
   and a.c_vip_id is not null
 group by c_vip_id, docno)
 group by c_vip_id
 ) aa
 left join bi_c_vip v on aa.c_vip_id = v.id
 left join bi_c_store c on v.c_store_id=c.id
 where 1=1
 # and (([frequency] =to_char(''1次'') and aa.qty=1) #
 # or ([frequency] =to_char(''2次'') and aa.qty=2) #
 # or ([frequency] =to_char(''3次'') and aa.qty=3) #
 # or ([frequency] =to_char(''大于3次'') and aa.qty>3)) # ', 'select c.name store_name,v.cardno,v.vipname,v.mobil,v.birthday
  # ,case when [frequency] is not null then aa.qty end frequency #
  from (
select c_vip_id, count(1) qty
  from (select c_vip_id, docno
          from ls_bill_d_retail a,BIFX_M_PRODUCT b
         where 1 = 1
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 # and a.billdate <= [jzDate] #
 # and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
 # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
   and a.status = 2
   and a.isactive = ''Y''
   and a.c_vip_id is not null
 group by c_vip_id, docno)
 group by c_vip_id
 ) aa
 left join bi_c_vip v on aa.c_vip_id = v.id
 left join bi_c_store c on v.c_store_id=c.id
 where 1=1
 # and (([frequency] =to_char(''1次'') and aa.qty=1) #
 # or ([frequency] =to_char(''2次'') and aa.qty=2) #
 # or ([frequency] =to_char(''3次'') and aa.qty=3) #
 # or ([frequency] =to_char(''大于3次'') and aa.qty>3)) # ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('30', '商品类别', 'splb', '13', '116', 'select id,attribname from m_dim where dimflag=''DIM6''', 'select id,attribname from m_dim where dimflag=''DIM6''', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员购买件数明细', 'hygmjsmx', '16', '109', 'select
 v.cardno,v.vipname,v.mobil,v.birthday,c.name store_name,aa.billdate,aa.docno
 # ,case when [pieceQty] is not null then aa.qty end piece_qty #
  from (
  select a.c_vip_id,a.docno,a.billdate, sum(qty) qty
          from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
         where 1=1
   and a.c_store_id=tx.c_store_id
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 # and a.billdate <= [jzDate] #
 # and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
 # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.c_vip_id is not null
         group by c_vip_id,a.docno,a.billdate) aa
 left join bi_c_vip v on aa.c_vip_id = v.id
 left join bi_c_store c on v.c_store_id=c.id
       where 1=1
  #    and (([pieceQty] =to_char(''1件'') and aa.qty=1) #
  #    or ([pieceQty] =to_char(''2件'') and aa.qty=2) #
  #    or ([pieceQty] =to_char(''3件'') and aa.qty=3) #
  #    or ([pieceQty] =to_char(''大于3件'') and aa.qty>3)) # ', 'select
 v.cardno,v.vipname,v.mobil,v.birthday,c.name store_name,aa.billdate,aa.docno
 # ,case when [pieceQty] is not null then aa.qty end piece_qty #
  from (
  select a.c_vip_id,a.docno,a.billdate, sum(qty) qty
          from ls_bill_d_retail a,BIFX_M_PRODUCT b
         where 1=1
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 # and a.billdate <= [jzDate] #
 # and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
 # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
 # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.c_vip_id is not null
         group by c_vip_id,a.docno,a.billdate) aa
 left join bi_c_vip v on aa.c_vip_id = v.id
 left join bi_c_store c on v.c_store_id=c.id
       where 1=1
  #    and (([pieceQty] =to_char(''1件'') and aa.qty=1) #
  #    or ([pieceQty] =to_char(''2件'') and aa.qty=2) #
  #    or ([pieceQty] =to_char(''3件'') and aa.qty=3) #
  #    or ([pieceQty] =to_char(''大于3件'') and aa.qty>3)) # ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员购买金额明细', 'hygmjemx', '16', '110', 'select v.cardno,v.vipname,v.mobil,v.birthday,c.name store_name
    # ,case when [amount] is not null then aa.tot_amt_actual end amount #
  from (
  select c_vip_id, sum(tot_amt_actual) tot_amt_actual
          from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
         where 1=1
   and a.c_store_id=tx.c_store_id
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.c_vip_id is not null
         group by c_vip_id
         ) aa
  left join bi_c_vip v
    on aa.c_vip_id = v.id
  left join bi_c_store c
    on v.c_store_id = c.id
 where 1=1
   #  and ((to_char(''大于500'') =[amount] and aa.tot_amt_actual > 500 ) #
   #   or (to_char(''200-500'') =[amount] and aa.tot_amt_actual between 200 and 500) #
   #   or (to_char(''小于200'') =[amount] and aa.tot_amt_actual<200)) # ', 'select v.cardno,v.vipname,v.mobil,v.birthday,c.name store_name
    # ,case when [amount] is not null then aa.tot_amt_actual end amount #
  from (
  select c_vip_id, sum(tot_amt_actual) tot_amt_actual
          from ls_bill_d_retail a,BIFX_M_PRODUCT b
         where 1=1
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.c_vip_id is not null
         group by c_vip_id
         ) aa
  left join bi_c_vip v
    on aa.c_vip_id = v.id
  left join bi_c_store c
    on v.c_store_id = c.id
 where 1=1
   #  and ((to_char(''大于500'') =[amount] and aa.tot_amt_actual > 500 ) #
   #   or (to_char(''200-500'') =[amount] and aa.tot_amt_actual between 200 and 500) #
   #   or (to_char(''小于200'') =[amount] and aa.tot_amt_actual<200)) # ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '折扣率分析', 'zklfx', '15', '112', 'select case
         when a.discount <= 0.3 then ''小于3折''
         when a.discount > 0.3 and a.discount <= 0.5 then ''3-5折''
         when a.discount > 0.5 and a.discount <= 0.7 then ''5-7折''
         when a.discount > 0.7 then ''大于7折''
       end name,
       count(1) qty
  from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where 1=1
   and a.c_store_id=tx.c_store_id
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.c_vip_id is not null
   and a.discount is not null
 group by case
            when a.discount <= 0.3 then ''小于3折''
            when a.discount > 0.3 and a.discount <= 0.5 then ''3-5折''
            when a.discount > 0.5 and a.discount <= 0.7 then ''5-7折''
            when a.discount > 0.7 then ''大于7折''
          end', 'select case
         when a.discount <= 0.3 then ''小于3折''
         when a.discount > 0.3 and a.discount <= 0.5 then ''3-5折''
         when a.discount > 0.5 and a.discount <= 0.7 then ''5-7折''
         when a.discount > 0.7 then ''大于7折''
       end name,
       count(1) qty
  from ls_bill_d_retail a,BIFX_M_PRODUCT b
 where 1=1
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.c_vip_id is not null
   and a.discount is not null
 group by case
            when a.discount <= 0.3 then ''小于3折''
            when a.discount > 0.3 and a.discount <= 0.5 then ''3-5折''
            when a.discount > 0.5 and a.discount <= 0.7 then ''5-7折''
            when a.discount > 0.7 then ''大于7折''
          end', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员积分', 'hyjf', '15', '114', 'select case
         when cc.ttl_integral < 1000 then ''小于1K''
         when cc.ttl_integral >= 1000 and cc.ttl_integral < 2000 then ''1K-2K''
         when cc.ttl_integral >= 2000 and cc.ttl_integral < 3000 then ''2K-3K''
         when cc.ttl_integral > 3000 then ''大于3K''
       end name,
       count(1) qty
  from (select distinct a.c_vip_id
    from ls_bill_d_retail a, BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
   where 1 = 1
     and a.c_store_id=tx.c_store_id
     and a.m_product_id = b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
     and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
     and a.c_vip_id is not null) a
  left join bi_c_vip cc
    on a.c_vip_id = cc.id
    where cc.ttl_integral>0
 group by case
            when cc.ttl_integral < 1000 then ''小于1K''
            when cc.ttl_integral >= 1000 and cc.ttl_integral < 2000 then ''1K-2K''
            when cc.ttl_integral >= 2000 and cc.ttl_integral < 3000 then ''2K-3K''
            when cc.ttl_integral > 3000 then ''大于3K''
          end ', 'select case
         when cc.ttl_integral < 1000 then ''小于1K''
         when cc.ttl_integral >= 1000 and cc.ttl_integral < 2000 then ''1K-2K''
         when cc.ttl_integral >= 2000 and cc.ttl_integral < 3000 then ''2K-3K''
         when cc.ttl_integral > 3000 then ''大于3K''
       end name,
       count(1) qty
  from (select distinct a.c_vip_id
    from ls_bill_d_retail a, BIFX_M_PRODUCT b
   where 1 = 1
     and a.m_product_id = b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
     and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
     and a.c_vip_id is not null) a
  left join bi_c_vip cc
    on a.c_vip_id = cc.id
    where cc.ttl_integral>0
 group by case
            when cc.ttl_integral < 1000 then ''小于1K''
            when cc.ttl_integral >= 1000 and cc.ttl_integral < 2000 then ''1K-2K''
            when cc.ttl_integral >= 2000 and cc.ttl_integral < 3000 then ''2K-3K''
            when cc.ttl_integral > 3000 then ''大于3K''
          end ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员购买周期明细', 'hygmzqmx', '16', '111', 'select
v.cardno,v.vipname,v.mobil,v.birthday,c.name store_name
 # ,case when [cycle] is not null then b.value end cycle #
  from member_buying_cycle b, table_vip_id vip,bi_c_vip v,c_store c,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where (+)b.c_vip_id = vip.c_vip_id
   and c.c_store_id=tx.c_store_id
 # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1 ) #
   and vip.c_vip_id=v.id(+)
   and v.c_store_id=c.id(+)
 # and (([cycle]=to_char(''小于3月'') and b.value<=3) #
 # or ([cycle]=to_char(''4-6月'') and b.value>3 and b.value<=6) #
 # or ([cycle]=to_char(''7-9月'') and b.value>6 and b.value<=9) #
 # or ([cycle]=to_char(''大于10月'') and b.value>9)) # ', 'select
v.cardno,v.vipname,v.mobil,v.birthday,c.name store_name
 # ,case when [cycle] is not null then b.value end cycle #
  from member_buying_cycle b, table_vip_id vip,bi_c_vip v,c_store c
 where (+)b.c_vip_id = vip.c_vip_id
 # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1 ) #
   and vip.c_vip_id=v.id(+)
   and v.c_store_id=c.id(+)
 # and (([cycle]=to_char(''小于3月'') and b.value<=3) #
 # or ([cycle]=to_char(''4-6月'') and b.value>3 and b.value<=6) #
 # or ([cycle]=to_char(''7-9月'') and b.value>6 and b.value<=9) #
 # or ([cycle]=to_char(''大于10月'') and b.value>9)) # ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '商品类别分析', 'splbfx', '15', '115', 'select c.attribname name, sum(a.qty) qty
  from ls_bill_d_retail a, BIFX_M_PRODUCT b, m_dim@portal80 c,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where 1 = 1
   and a.c_store_id=tx.c_store_id
   and a.m_product_id = b.id
   and b.m_dim6_id = c.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''
 group by c.attribname', 'select c.attribname name, sum(a.qty) qty
  from ls_bill_d_retail a, BIFX_M_PRODUCT b, m_dim@portal80 c
 where 1 = 1
   and a.m_product_id = b.id
   and b.m_dim6_id = c.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   and a.billdate >= 20180101
   and a.billdate < to_char(sysdate,''YYYYMMDD'')
   and a.isactive = ''Y''
 group by c.attribname', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员购买件数', 'hygmjs', '16', '120', 'select case
         when qty = 1 then ''1件''
         when qty = 2 then ''2件''
         when qty = 3 then ''3件''
         else ''大于3件''
       end name,
       count(1) qty
  from (select c_vip_id, sum(qty) qty
          from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
         where 1=1
   and a.c_store_id=tx.c_store_id
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.c_vip_id is not null
         group by c_vip_id)
group by case
         when qty = 1 then ''1件''
         when qty = 2 then ''2件''
         when qty = 3 then ''3件''
         else ''大于3件''
       end', 'select case
         when qty = 1 then ''1件''
         when qty = 2 then ''2件''
         when qty = 3 then ''3件''
         else ''大于3件''
       end name,
       count(1) qty
  from (select c_vip_id, sum(qty) qty
          from ls_bill_d_retail a,BIFX_M_PRODUCT b
         where 1=1
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.c_vip_id is not null
         group by c_vip_id)
group by case
         when qty = 1 then ''1件''
         when qty = 2 then ''2件''
         when qty = 3 then ''3件''
         else ''大于3件''
       end', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员购买金额', 'hygmje', '16', '121', 'select case
         when tot_amt_actual <200 then ''小于200$''
         when tot_amt_actual >=200 and tot_amt_actual<500 then ''200$-500$''
         when tot_amt_actual >=500 then ''大于500$''
       end name,
       count(1) qty
  from (select c_vip_id, sum(tot_amt_actual) tot_amt_actual
          from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
         where 1=1
   and a.c_store_id=tx.c_store_id
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.c_vip_id is not null
         group by c_vip_id)
group by case
         when tot_amt_actual <200 then ''小于200$''
         when tot_amt_actual >=200 and tot_amt_actual<500 then ''200$-500$''
         when tot_amt_actual >=500 then ''大于500$''
       end
       ', 'select case
         when tot_amt_actual <200 then ''小于200$''
         when tot_amt_actual >=200 and tot_amt_actual<500 then ''200$-500$''
         when tot_amt_actual >=500 then ''大于500$''
       end name,
       count(1) qty
  from (select c_vip_id, sum(tot_amt_actual) tot_amt_actual
          from ls_bill_d_retail a,BIFX_M_PRODUCT b
         where 1=1
   and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.c_vip_id is not null
         group by c_vip_id)
group by case
         when tot_amt_actual <200 then ''小于200$''
         when tot_amt_actual >=200 and tot_amt_actual<500 then ''200$-500$''
         when tot_amt_actual >=500 then ''大于500$''
       end
       ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('30', '风格类别', 'fglb', '13', '123', 'select id,attribname from m_dim where dimflag=''DIM8''', 'select id,attribname from m_dim where dimflag=''DIM8''', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员购买周期', 'hygmzq', '16', '122', 'select case
         when b.value <= 3 then ''小于3月''
         when b.value > 3 and b.value <= 6 then ''4-6月''
         when b.value > 6 and b.value <= 9 then ''7-9月''
         when b.value > 9 then ''大于10月''
       end name,
       count(b.c_vip_id) qty
  from member_buying_cycle b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx,table_vip_id vip
 where b.c_vip_id =vip.c_vip_id
   and b.c_store_id=tx.c_store_id
 # AND a.c_store_id IN (WITH GS AS (SELECT [ storeIds ] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1 ) #
 group by case
            when b.value <= 3 then ''小于3月''
            when b.value > 3 and b.value <= 6 then ''4-6月''
            when b.value > 6 and b.value <= 9 then ''7-9月''
            when b.value > 9 then ''大于10月''
          end', 'select case
         when b.value <= 3 then ''小于3月''
         when b.value > 3 and b.value <= 6 then ''4-6月''
         when b.value > 6 and b.value <= 9 then ''7-9月''
         when b.value > 9 then ''大于10月''
       end name,
       count(b.c_vip_id) qty
  from member_buying_cycle b,table_vip_id vip
 where b.c_vip_id =vip.c_vip_id
 # AND a.c_store_id IN (WITH GS AS (SELECT [ storeIds ] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1 ) #
 group by case
            when b.value <= 3 then ''小于3月''
            when b.value > 3 and b.value <= 6 then ''4-6月''
            when b.value > 6 and b.value <= 9 then ''7-9月''
            when b.value > 9 then ''大于10月''
          end', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', 'VIP卡类型', 'vipklx', '13', '124', 'select id,name,c_customer_id from C_VIPTYPE@portal80
 # where c_customer_id in ([customerId]) #
order by c_customer_id', 'select id,name,c_customer_id from C_VIPTYPE@portal80
 # where c_customer_id in ([customerId]) #
order by c_customer_id', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员挖掘店仓信息', 'hywjdcxx', '7', '125', 'select a.id, a.name, a.c_customer_id,a.c_customerup_id
  from bs_c_store a,(select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where a.isactive = ''Y''
   and a.id=tx.c_store_id
   and a.name not like ''%测试%''
   and type_id = 1
 #  and ( a.c_customer_id in ([customerId]) #
 # or a.c_customerup_id in ([customerId]) ) #
order by name', 'select a.id, a.name, a.c_customer_id,a.c_customerup_id
  from bs_c_store a
 where a.isactive = ''Y''
   and a.name not like ''%测试%''
   and type_id = 1
 #  and ( a.c_customer_id in ([customerId]) #
 # or a.c_customerup_id in ([customerId]) ) #
order by name
', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员挖掘', 'hywj', '7', '126', 'select a.customer_name customer,
       a.store_name store,
       a.cardno card,
       a.vipname nick_name,
       a.mobil mobel,
       a.end_integral integral,
       a.viptype vip_type
  from vip_bs_vipinfo               a,
       bi_vip_sale_excavate_price   b,
       bi_vip_sale_excavate_product c,
       bi_m_product_vip m,
       bi_vip_retail v,
       (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where a.vip_id = b.c_vip_id
   and a.vip_id = c.c_vip_id
   and c.m_product_id=m.id
   and a.vip_id=v.c_vip_id
   and a.mobil is not null
   and a.c_store_id = tx.c_store_id
 # and not exists (select 1 from ls_bill_d_retail x where 1 = 1 and x.billdate >= substr([outDate], #
 # instr([outDate],''+'')+1) and x.c_vip_id is not null and x.c_vip_id = a.vip_id group by x.c_vip_id) #
   and exists (select 1
                     from ls_bill_d_retail xx
                    where 1 = 1
                    # and xx.billdate >= substr([inDate], #
                    # instr([inDate],''+'')+1) #
                    # and xx.sizes in ([size]) #
                    # and xx.season_id in ([mid9]) #
                    # and xx.discount >= [minDiscount] #
                    # and xx.discount <= [maxDiscount] #
                    # and xx.color_name like ''%''||[color]||''%'' #
                      and xx.c_vip_id = a.vip_id
                      and xx.c_vip_id is not null
                    group by xx.c_vip_id)
 # and exists (select distinct c_vip_id from bi_vip_sale_excavate_double a where a.qty > [double]) #
 # and a.c_customer_id in ([customer]) #
 # and m.m_dim8_id in ([mid8]) #
 # and b.min_price >= [minPrice] #
 # and b.max_price <= [maxPrice] #
 # and a.integral> [integral] #
 # and a.c_store_id in ([store]) #
 # and a.c_viptype_id in ([vipType]) #
 group by a.customer_name,a.store_name,a.cardno,a.vipname,a.mobil,a.end_integral,a.viptype
 order by a.customer_name,a.store_name,a.cardno desc,a.vipname,a.mobil,a.end_integral,a.viptype', 'select a.customer_name customer,
       a.store_name store,
       a.cardno card,
       a.vipname nick_name,
       a.mobil mobel,
       a.end_integral integral,
       a.viptype vip_type
  from vip_bs_vipinfo               a,
       bi_vip_sale_excavate_price   b,
       bi_vip_sale_excavate_product c,
       bi_m_product_vip m,
       bi_vip_retail v
 where a.vip_id = b.c_vip_id
   and a.vip_id = c.c_vip_id
   and c.m_product_id=m.id
   and a.vip_id=v.c_vip_id
   and a.mobil is not null
 # and not exists (select 1 from ls_bill_d_retail x where 1 = 1 and x.billdate >= substr([outDate], #
 # instr([outDate],''+'')+1) and x.c_vip_id is not null and x.c_vip_id = a.vip_id group by x.c_vip_id) #
   and exists (select 1
       from ls_bill_d_retail xx
      where 1 = 1
      # and xx.billdate >= substr([inDate], #
      # instr([inDate],''+'')+1) #
      # and xx.sizes in ([size]) #
      # and xx.season_id in ([mid9]) #
      # and xx.discount >= [minDiscount] #
      # and xx.discount <= [maxDiscount] #
      # and xx.color_name like ''%''||[color]||''%'' #
        and xx.c_vip_id = a.vip_id
        and xx.c_vip_id is not null
      group by xx.c_vip_id)
 # and exists (select distinct c_vip_id from bi_vip_sale_excavate_double a where a.qty > [double]) #
 # and a.c_customer_id in ([customer]) #
 # and m.m_dim8_id in ([mid8]) #
 # and b.min_price >= [minPrice] #
 # and b.max_price <= [maxPrice] #
 # and a.integral> [integral] #
 # and a.c_store_id in ([store]) #
 # and a.c_viptype_id in ([vipType]) #
 group by a.customer_name,a.store_name,a.cardno,a.vipname,a.mobil,a.end_integral,a.viptype
 order by a.customer_name,a.store_name,a.cardno desc,a.vipname,a.mobil,a.end_integral,a.viptype', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '经销商套餐分析', 'jxstcfx', '8', '131', 'select a.*,b.vip_sum,b.gic_vip_num,
round(decode(b.vip_sum,0,0,nvl((b.vip_sum-b.gic_vip_num),0)/b.vip_sum)*100,2) not_gic_proportion
 from (
select c.c_customer_name customer,
       a.c_customer_id customer_id,
     # case when [customerId] is not null then store_name end store, #
     # case when [customerId] is not null then a.c_store_id end c_store_id, #
       sum(a.sales_sum) sales_sum,
       sum(a.tot_amt_actual) tot_amt_actual,
       sum(a.setmeal_amt_actual) setmeal_amt_actual,       round(decode(sum(a.tot_amt_actual),0,0,nvl(sum(a.setmeal_amt_actual),0)/sum(a.tot_amt_actual))*100,2) setmeal_proportion,
       sum(a.setmeal_sum) setmeal_sum,
       count(distinct (case when a.setmeal_sum>0 then a.c_store_id end)) setmeal_store_sum,
       sum(a.vous_sum) vous_sum,
       sum(a.vous_joint) vous_joint,
       round(decode(sum(a.setmeal_sum),0,0,nvl(sum(a.vous_joint),0)/sum(a.setmeal_sum))*100,2) vous_joint_reat,
       sum(a.vous_repurchas) vous_repurchas,
       round(decode(sum(a.sales_sum),0,0,nvl(sum(a.vous_repurchas),0)/sum(a.sales_sum))*100,2) vous_repurchas_reat,       round(decode(sum(a.tot_amt_actual),0,0,nvl(sum(a.vous_repurchas_amt),0)/sum(a.tot_amt_actual))*100,2) vous_repurchas_proportion,
       sum(a.vous_repurchas_amt) vous_repurchas_amt,
       sum(a.not_vip_sum) not_vip_sum,
       sum(a.not_vip_amt_actual) not_vip_amt_actual,
       round(decode(sum(a.tot_amt_actual),0,0,nvl(sum(a.not_vip_amt_actual),0)/sum(a.tot_amt_actual))*100,2) not_vip_amt_actual_proportion,
       sum(a.vous_amt_actual) vous_amt_actual,
       round(decode(sum(a.tot_amt_actual),0,0,nvl(sum(a.vous_amt_actual),0)/sum(a.tot_amt_actual)) *100,2) vous_amt_actual_proportion
  from customer_setmeal_analysis a,
       (select * from table(f_store_table(''@fdd''))) c
 where 1=1
   and a.c_store_id=c.c_store_id
 # and a.billdate between [beginDate]  #
 # and [endDate] #
 # and a.c_customer_id=[customerId] #
 group by c.c_customer_name,a.c_customer_id
  # ,case when [customerId] is not null then store_name end  #
  # ,case when [customerId] is not null then a.c_store_id end  #
  ) a,
  (select s.id c_customer_id,
             # case when [customerId] is not null then c.id  end c_store_id, #
               count(distinct v.id) vip_sum,
               count(distinct(case when length(v.service_open_id) > 2 then v.id end)) gic_vip_num
          from m_retail@portal80      a,
               c_store@portal80       c,
               c_customer@portal80    s,
               sjfx_customer@portal80 sn,
               c_client_vip@portal80  v
         where a.c_store_id = c.id
           and c.c_customer_id = sn.id
           and sn.customer_id = s.id
           and a.c_vip_id = v.id
           and a.isactive = ''Y''
           and a.status = 2
           and c.C_STOREKIND_ID = 1
           and s.c_cusrank_id = 2
           and a.c_vip_id is not null
         # and s.id = [customerId] #
         # and a.billdate between [beginDate] #
         # and [endDate] #
         group by s.id
         # ,case when [customerId] is not null then c.id  end  #
         ) b
   where a.customer_id=b.c_customer_id
   # and a.c_store_id= (case when [customerId] is not null then  b.c_store_id end) #
 order by a.setmeal_amt_actual desc', 'select a.*,
b.vip_sum,b.gic_vip_num,
round(decode(b.vip_sum,0,0,nvl((b.vip_sum-b.gic_vip_num),0)/b.vip_sum)*100,2) not_gic_proportion
from (
select s.name customer,
       a.c_customer_id customer_id,
     # case when [customerId] is not null then c.name end store, #
     # case when [customerId] is not null then c.id end c_store_id, #
       sum(a.sales_sum) sales_sum,
       sum(a.tot_amt_actual) tot_amt_actual,
       sum(a.setmeal_amt_actual) setmeal_amt_actual,       round(decode(sum(a.tot_amt_actual),0,0,nvl(sum(a.setmeal_amt_actual),0)/sum(a.tot_amt_actual))*100,2) setmeal_proportion,
       sum(a.setmeal_sum) setmeal_sum,
       count(distinct (case when a.setmeal_sum>0 then a.c_store_id end)) setmeal_store_sum,
       sum(a.vous_sum) vous_sum,
       sum(a.vous_joint) vous_joint,
       round(decode(sum(a.setmeal_sum),0,0,nvl(sum(a.vous_joint),0)/sum(a.setmeal_sum))*100,2) vous_joint_reat,
       sum(a.vous_repurchas) vous_repurchas,
       round(decode(sum(a.sales_sum),0,0,nvl(sum(a.vous_repurchas),0)/sum(a.sales_sum))*100,2) vous_repurchas_reat,       round(decode(sum(a.tot_amt_actual),0,0,nvl(sum(a.vous_repurchas_amt),0)/sum(a.tot_amt_actual))*100,2) vous_repurchas_proportion,
       sum(a.vous_repurchas_amt) vous_repurchas_amt,
       sum(a.not_vip_sum) not_vip_sum,
       sum(a.not_vip_amt_actual) not_vip_amt_actual,
       round(decode(sum(a.tot_amt_actual),0,0,nvl(sum(a.not_vip_amt_actual),0)/sum(a.tot_amt_actual))*100,2) not_vip_amt_actual_proportion,
       sum(a.vous_amt_actual) vous_amt_actual,
       round(decode(sum(a.tot_amt_actual),0,0,nvl(sum(a.vous_amt_actual),0)/sum(a.tot_amt_actual)) *100,2) vous_amt_actual_proportion
  from customer_setmeal_analysis a,c_customer@portal80 s,c_store@portal80 c
 where 1=1
   and a.c_customer_id=s.id
   and a.c_store_id=c.id
 # and a.billdate between [beginDate]  #
 # and [endDate] #
 # and a.c_customer_id=[customerId] #
 group by s.name,
     a.c_customer_id
  # ,case when [customerId] is not null then c.name end  #
  # ,case when [customerId] is not null then c.id end  #
  ) a ,
  (select s.id c_customer_id,
             # case when [customerId] is not null then c.id  end c_store_id, #
               count(distinct v.id) vip_sum,
               count(distinct(case when length(v.service_open_id) > 2 then v.id end)) gic_vip_num
          from m_retail@portal80      a,
               c_store@portal80       c,
               c_customer@portal80    s,
               sjfx_customer@portal80 sn,
               c_client_vip@portal80  v
         where a.c_store_id = c.id
           and c.c_customer_id = sn.id
           and sn.customer_id = s.id
           and a.c_vip_id = v.id
           and a.isactive = ''Y''
           and a.status = 2
           and c.C_STOREKIND_ID = 1
           and s.c_cusrank_id = 2
           and a.c_vip_id is not null
         # and s.id = [customerId] #
         # and a.billdate between [beginDate] #
         # and [endDate] #
         group by s.id
         # ,case when [customerId] is not null then c.id  end  #
         ) b
 where a.customer_id=b.c_customer_id
   # and a.c_store_id= (case when [customerId] is not null then  b.c_store_id end) #
 order by a.setmeal_amt_actual desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('80', '员工工龄比例', 'ygglbl', '9', '139', 'select case
        when aa.age <= 3 then ''小于3年''
        when aa.age between 4 and 6 then ''4-6年''
        when aa.age between 7 and 10 then ''7-10年''
        when aa.age >= 11 then ''11年以上''
      end name,
      count(1) value
 from (select year(getdate()) - substring(a.startdate, 1, 4) + 1 age
         from t_employee a
        where a.startdate is not null
          and a.startdate <> '''') aa
group by case
           when aa.age <= 3 then ''小于3年''
           when aa.age between 4 and 6 then ''4-6年''
           when aa.age between 7 and 10 then ''7-10年''
           when aa.age >= 11 then ''11年以上''
         end
', 'select case
        when aa.age <= 3 then ''小于3年''
        when aa.age between 4 and 6 then ''4-6年''
        when aa.age between 7 and 10 then ''7-10年''
        when aa.age >= 11 then ''11年以上''
      end name,
      count(1) value
 from (select year(getdate()) - substring(a.startdate, 1, 4) + 1 age
         from t_employee a
        where a.startdate is not null
          and a.startdate <> '''') aa
group by case
           when aa.age <= 3 then ''小于3年''
           when aa.age between 4 and 6 then ''4-6年''
           when aa.age between 7 and 10 then ''7-10年''
           when aa.age >= 11 then ''11年以上''
         end
', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '系统客户日均单店', 'xtkhrjdd', '10', '142', '  select cu.c_customer_name customer,
       dd.tot_amt_actual,
       dd.tot_retail_amt_actual,
       dd.tot_retail_qty,
       dd.day_store_avg_sum,
       dd.day_recharge_avg_sum,
       dd.store_count,
       dd.store_retail_count,
       dd.store_recharge_count,
       dd.day_avg_sale,
       dd.day_retail_avg_sale,
       dd.day_recharge_avg_sale,
       dd.day_avg_sum,
       dd.day_avg_price1,
       ee.day_avg_price,
       ee.tot_amt_actual store_avg_tot_amt_actual,
       ee.qty store_avg_qty,
       mm.mon_store_sum1,
       mm.mon_store_sum11,
       mm.mon_store_sum12,
       mm.mon_store_sum2,
       mm.increase,
       gg.old_year_day_avg1,
       gg.old_year_day_avg11,
       gg.old_year_day_avg12,
       gg.old_year_day_avg2,
       gg.old_day_avg_increase1,
       ff.old_day_avg1,
       ff.old_day_avg11,
       ff.old_day_avg12,
       ff.old_day_avg2,
       ff.old_day_avg_increase2,
       dense_rank() over(order by day_avg_sale desc) as day_top
from (select cu.c_customer_id,cu.c_customer_name,cu.business_type
       from view_bs_c_CUSTOMER cu
       where exists (select 1
                       from table(f_store_table(''@fdd''))fst
                      where fst.c_customer_id=cu.c_customer_id))cu,
(select c_customer_id,
        tot_amt_actual,
        tot_retail_amt_actual,
        tot_retail_qty,
        day_store_avg_sum,
        day_recharge_avg_sum,
        store_count,
        store_retail_count,
        store_recharge_count,
       round(decode(store_count,0,0,tot_amt_actual/store_count)) day_avg_sale,
       round(decode(store_retail_count, 0, 0, day_store_avg_sum / store_retail_count)) day_retail_avg_sale,
       round(decode(store_recharge_count, 0, 0, day_recharge_avg_sum / store_retail_count)) day_recharge_avg_sale,
       round(decode(store_count,0,0,qty/store_count)) day_avg_sum,
       round(decode(qty,0,0,tot_amt_actual/qty)) day_avg_price,
       round(decode(tot_retail_qty, 0, 0, tot_retail_amt_actual / tot_retail_qty)) day_avg_price1
from
  (select s.c_customer_id,sum(s.tot_amt_actual) tot_amt_actual,
 sum(case when (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0 then s.tot_amt_actual end) tot_retail_amt_actual,
          sum(case when s.m_product_name not like ''ZS%'' then s.tot_amt_actual end) day_store_avg_sum,
          nvl(sum(case when s.m_product_name like ''ZS%'' then s.tot_amt_actual end),0) day_recharge_avg_sum,
         sum(s.qty) tot_qty,
    sum(case when (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0 then s.qty end) tot_retail_qty,
         sum(case when s.m_product_name not like ''ZS%'' then s.qty end) qty,
count(distinct(s.c_store_id)) store_count,
   count(distinct case when s.m_product_name not like ''ZS%'' then s.c_store_id end) store_retail_count,
   count(distinct case when s.m_product_name like ''ZS%'' then s.c_store_id end) store_recharge_count
    from LS_BILL_D_RETAIL s,
         v_bs_c_store t,
         t_day d,
        (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
           from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
          where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)spsx
   where 1=1
     and s.c_store_id = t.id
     and s.billdate=d.id
     and s.m_product_id=spsx.id
 #   and d.t_day_day = to_date([billdate],''yyyymmdd'') #
     and t.business <> ''加盟店''
     and t.name not like ''%仓%''
     and t.name not like ''%特卖%''
      and t.name not like ''%福利%''
     and spsx.M_DIM18_ID in (240,241)
   group by s.c_customer_id))dd,

 (select c_customer_id,
         tot_amt_actual,
         qty,
         round(decode(qty, 0, 0, tot_amt_actual / qty)) day_avg_price
    from (select s.c_customer_id,
                 sum(s.tot_amt_actual) tot_amt_actual,
                 sum(s.qty) qty
            from LS_BILL_D_RETAIL s,v_bs_c_store t,t_day d,
                 (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
                    from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
                   where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
           where 1 = 1
             and s.c_store_id = t.id
             and s.billdate = d.id
             and s.m_product_id = spsx.id
             and d.t_day_day = to_date([billdate], ''yyyymmdd'')
             and t.business <> ''加盟店''
             and t.name not like ''%仓%''
             and t.name not like ''%特卖%''
             and t.name not like ''%福利%''
             and spsx.M_DIM18_ID in (240, 241)
             and not exists (select 1 from LS_BILL_D_RETAIL a where  a.m_product_name like ''ZS%'' and a.billdate=[billdate] and a.id=s.id )
           group by s.c_customer_id)) ee,

(select (case when a.c_customer_id is null then b.c_customer_id else a.c_customer_id end) c_customer_id,
       nvl(mon_store_sum1,0) mon_store_sum1,
       nvl(mon_store_sum11,0) mon_store_sum11,
       nvl(mon_store_sum12,0) mon_store_sum12,
       nvl(mon_store_sum2,0) mon_store_sum2,
       round(decode(nvl(mon_store_sum2,0),0,0,((nvl(mon_store_sum1,0)-nvl(mon_store_sum2,0))/nvl(mon_store_sum2,0))*100))||''%'' increase
from
(select c_customer_id,
        sum(tot_amt_actual_this) tot_amt_actual_this,
        sum(tot_amt_actual_this1) tot_amt_actual_this1,
        sum(tot_amt_actual_this2) tot_amt_actual_this2,
        sum(store_count_this) store_count_this,
        sum(store_count_this1) store_count_this1,
        sum(store_count_this2) store_count_this2,
        sum(sum_date) sum_date,
        sum(sum_date1) sum_date1,
        sum(sum_date2) sum_date2,
 #  round(decode(substr([billdate], -2),0,0,(decode(sum(store_count_this),0,0,sum(tot_amt_actual_this) / sum(sum_date))))) mon_store_sum1, #
 # round(decode(substr([billdate], -2),0,0,(decode(sum(store_count_this),0,0,sum(tot_amt_actual_this1) / sum(sum_date))))) mon_store_sum11, #
 # round(decode(substr([billdate], -2),0,0,(decode(sum(store_count_this),0,0,sum(tot_amt_actual_this2) / sum(sum_date))))) mon_store_sum12 #
  from
    (select m.c_customer_id,
            m.c_store_id,
           sum(m.tot_amt_actual) tot_amt_actual_this,
           sum(case when m.m_product_name not like ''ZS%'' then m.tot_amt_actual end) tot_amt_actual_this1,
           nvl(sum(case when m.m_product_name like ''ZS%'' then m.tot_amt_actual end),0) tot_amt_actual_this2,
           count(distinct m.c_store_id) store_count_this,
           count(case when m.m_product_name not like ''ZS%'' then m.c_store_id end) store_count_this1,
           count(case when m.m_product_name like ''ZS%'' then m.c_store_id end) store_count_this2,
           count(distinct m.billdate) sum_date,
           count(distinct case when m.m_product_name not like ''ZS%'' then m.billdate end) sum_date1,
           count(distinct case when m.m_product_name like ''ZS%'' then m.billdate end) sum_date2
    from LS_BILL_D_RETAIL m,
         v_bs_c_store t,
         (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
            from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
           where t.M_DIM2_ID=y.PRODUCT_YEAR_ID) spsx
    where 1=1
      and m.c_store_id = t.id
      and m.m_product_id=spsx.id
 #    and m.billdate between substr([billdate],0,6)||''01'' #
 #    and [billdate] #
      and t.business <> ''加盟店''
      and t.name not like ''%仓%''
      and t.name not like ''%特卖%''
      and t.name not like ''%福利%''
      and spsx.M_DIM18_ID in (240,241)
    group by m.c_customer_id,m.c_store_id
   having sum(m.tot_amt_actual)<>0)
    group by c_customer_id
)a
full outer join
(

select c_customer_id,
       sum(tot_amt_actual_this) tot_amt_actual_this,
       sum(tot_amt_actual_this1) tot_amt_actual_this1,
       sum(tot_amt_actual_this2) tot_amt_actual_this2,
       sum(store_count_this) store_count_this,
 # round(decode(substr(to_char(add_months(to_date([billdate],''yyyymmdd''),-24),''yyyymmdd''),-2),0,0,(decode(sum(store_count_this),0,0,sum(tot_amt_actual_this)/sum(sum_date))))) mon_store_sum2 #
  from
    (select m.c_customer_id,m.c_store_id,
           sum(m.tot_amt_actual) tot_amt_actual_this,
           sum(case when m.m_product_name not like ''ZS%'' then m.tot_amt_actual end) tot_amt_actual_this1,
           nvl(sum(case when m.m_product_name like ''ZS%'' then m.tot_amt_actual end),0) tot_amt_actual_this2,
           count(distinct m.c_store_id) store_count_this,
           count(distinct m.billdate) sum_date
    from LS_BILL_D_RETAIL m,
         v_bs_c_store t,
        (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
           from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
          where t.M_DIM2_ID=y.PRODUCT_YEAR_ID) spsx
    where 1=1
      and m.c_store_id = t.id
      and m.m_product_id=spsx.id
 # and m.billdate between substr(to_char(add_months(to_date([billdate],''yyyymmdd''),-24),''yyyymmdd''),0,6)||''01''  #
 #    and to_char(add_months(to_date([billdate],''yyyymmdd''),-24),''yyyymmdd'') #
      and t.business <> ''加盟店''
      and t.name not like ''%仓%''
      and t.name not like ''%特卖%''
      and t.name not like ''%福利%''
      and spsx.M_DIM18_ID in (240,241)
    group by m.c_customer_id,m.c_store_id
   having sum(m.tot_amt_actual)<>0)
    group by c_customer_id
)b
on a.c_customer_id=b.c_customer_id) mm,

(
select c_customer_id,
       round(avg(avg_new)) old_year_day_avg1,
       round(avg(avg_new1)) old_year_day_avg11,
       round(avg(avg_new2)) old_year_day_avg12,
       round(avg(avg_old)) old_year_day_avg2,
       round(decode(nvl(avg(avg_old),0),0,0,((nvl(avg(avg_new),0)-nvl(avg(avg_old),0))/nvl(avg(avg_old),0))*100))||''%'' old_day_avg_increase1
 from (
select a.c_customer_id,
       a.c_store_id,
       a.tot_amt_actual_this tot_amt_acutal_new,
       a.store_count_this store_count_new,
       a.sum_date sum_new,
       a.amt_actual_store amt_actual_new,
       round(a.tot_amt_actual_this/a.sum_date,2) avg_new,
       round(a.tot_amt_actual_this1/a.sum_date,2) avg_new1,
       round(a.tot_amt_actual_this2/a.sum_date,2) avg_new2,
       b.tot_amt_actual_this tot_amt_acutal_old,
       b.store_count_this store_count_old,
       b.sum_date sum_old,
       b.amt_actual_store amt_actual_old,
       round(b.tot_amt_actual_this/b.sum_date,2) avg_old
from (
select m.c_customer_id,
       m.c_store_id,
       sum(m.tot_amt_actual) tot_amt_actual_this,
       sum(case when m.m_product_name not like ''ZS%'' then m.tot_amt_actual end) tot_amt_actual_this1,
       nvl(sum(case when m.m_product_name like ''ZS%'' then m.tot_amt_actual end),0) tot_amt_actual_this2,
       count(distinct(m.c_store_id)) store_count_this,
       count(distinct m.billdate) sum_date,
       count(distinct case when m.m_product_name not like ''ZS%'' then m.billdate end) sum_date1,
       count(distinct case when m.m_product_name like ''ZS%'' then m.billdate end) sum_date2,
       round(decode(count(distinct m.billdate),0,0,sum(m.tot_amt_actual) / count(distinct m.billdate)),2) amt_actual_store
  from LS_BILL_D_RETAIL m,
       v_bs_c_store t,
       (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
          from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
         where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
 where 1 = 1
   and m.c_store_id = t.id
   and m.m_product_id = spsx.id
 # and m.billdate between substr([billdate], 0, 6)||''01'' #
 # and [billdate] #
 # and to_date([billdate], ''yyyymmdd'') - to_date(t.rentbegin, ''yyyymmdd'') > 365 #
   and t.business <> ''加盟店''
   and t.name not like ''%仓%''
   and t.name not like ''%特卖%''
   and t.name not like ''%福利%''
   and t.name not like ''%微信%''
   and spsx.M_DIM18_ID in (240, 241)
 group by m.c_customer_id, m.c_store_id
having sum(m.tot_amt_actual)<>0) a
inner join
(select m.c_customer_id,
       m.c_store_id,
       sum(m.tot_amt_actual) tot_amt_actual_this,
       count(distinct(m.c_store_id)) store_count_this,
       count(distinct m.billdate) sum_date,
       round(decode(count(distinct m.billdate),0,0,sum(m.tot_amt_actual) / count(distinct m.billdate)),2) amt_actual_store
  from LS_BILL_D_RETAIL m,
       v_bs_c_store t,
       (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
          from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
         where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
 where 1 = 1
   and m.c_store_id = t.id
   and m.m_product_id = spsx.id
 # and m.billdate between substr(to_char(add_months(to_date([billdate], ''yyyymmdd''), -24),''yyyymmdd''),0,6) || ''01'' #
 # and to_char(add_months(to_date([billdate], ''yyyymmdd''), -24), ''yyyymmdd'') #
 # and to_date([billdate], ''yyyymmdd'') - to_date(t.rentbegin, ''yyyymmdd'') > 365 #
   and t.business <> ''加盟店''
   and t.name not like ''%仓%''
   and t.name not like ''%特卖%''
   and t.name not like ''%福利%''
   and t.name not like ''%微信%''
   and spsx.M_DIM18_ID in (240, 241)
 group by m.c_customer_id, m.c_store_id
having sum(m.tot_amt_actual)<>0) b
 on a.c_customer_id=b.c_customer_id and a.c_store_id=b.c_store_id)
 group by c_customer_id)gg,
(select
a.c_customer_id,
round(avg(a.tot_amt_actual)) old_day_avg1,
round(avg(a.tot_amt_actual1)) old_day_avg11,
round(avg(a.tot_amt_actual2)) old_day_avg12,
round(avg(b.tot_amt_actual)) old_day_avg2,
round(decode(nvl(avg(b.tot_amt_actual),0),0,0,((nvl(avg(a.tot_amt_actual),0)-nvl(avg(b.tot_amt_actual),0))/nvl(avg(b.tot_amt_actual),0))*100))||''%'' old_day_avg_increase2
from (
select s.c_customer_id,
       s.c_store_id,
       sum(s.tot_amt_actual) tot_amt_actual,
       sum(case when s.m_product_name not like ''ZS%'' then s.tot_amt_actual end) tot_amt_actual1,
       nvl(sum(case when s.m_product_name like ''ZS%'' then s.tot_amt_actual end),0) tot_amt_actual2
  from LS_BILL_D_RETAIL s,
       v_bs_c_store t,
       (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
          from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
         where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
 where 1 = 1
   and s.c_store_id = t.id
   and s.m_product_id = spsx.id
 # and s.billdate = [billdate] #
 # and to_date([billdate], ''yyyymmdd'') - to_date(t.rentbegin, ''yyyymmdd'') > 365 #
   and t.business <> ''加盟店''
   and t.name not like ''%仓%''
   and t.name not like ''%福利%''
   and t.name not like ''%特卖%''
   and spsx.M_DIM18_ID in (240, 241)
 group by s.c_customer_id, s.c_store_id
having sum(s.tot_amt_actual)<>0) a
inner join
(
select s.c_customer_id,
       s.c_store_id,
       sum(s.tot_amt_actual) tot_amt_actual,
       sum(case when s.m_product_name not like ''ZS%'' then s.tot_amt_actual end) tot_amt_actual1,
       sum(case when s.m_product_name like ''ZS%'' then s.tot_amt_actual end) tot_amt_actual2
  from LS_BILL_D_RETAIL s,
       v_bs_c_store t,
       (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
          from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
         where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
 where 1 = 1
   and s.c_store_id = t.id
   and s.m_product_id = spsx.id
 # and s.billdate =to_char(add_months(to_date([billdate], ''yyyymmdd''),-24), ''yyyymmdd'')  #
 # and to_date([billdate], ''yyyymmdd'') -to_date(t.rentbegin, ''yyyymmdd'') > 365 #
   and t.business <> ''加盟店''
   and t.name not like ''%仓%''
   and t.name not like ''%特卖%''
   and t.name not like ''%福利%''
   and spsx.M_DIM18_ID in (240, 241)
 group by s.c_customer_id,s.c_store_id
having sum(s.tot_amt_actual)<>0) b
 on a.c_customer_id=b.c_customer_id and a.c_store_id=b.c_store_id
 group by a.c_customer_id) ff
where cu.c_customer_id=dd.c_customer_id
  and dd.c_customer_id=ee.c_customer_id(+)
  and dd.c_customer_id=mm.c_customer_id(+)
  and dd.c_customer_id=gg.c_customer_id(+)
  and dd.c_customer_id=ff.c_customer_id(+)
order by day_avg_sale desc', '  select cu.c_customer_name customer,
       dd.tot_amt_actual,
       dd.tot_retail_amt_actual,
       dd.tot_retail_qty,
       dd.day_store_avg_sum,
       dd.day_recharge_avg_sum,
       dd.store_count,
       dd.store_retail_count,
       dd.store_recharge_count,
       dd.day_avg_sale,
       dd.day_retail_avg_sale,
       dd.day_recharge_avg_sale,
       dd.day_avg_sum,
       dd.day_avg_price1,
       ee.day_avg_price,
       ee.tot_amt_actual store_avg_tot_amt_actual,
       ee.qty store_avg_qty,
       mm.mon_store_sum1,
       mm.mon_store_sum11,
       mm.mon_store_sum12,
       mm.mon_store_sum2,
       mm.increase,
       gg.old_year_day_avg1,
       gg.old_year_day_avg11,
       gg.old_year_day_avg12,
       gg.old_year_day_avg2,
       gg.old_day_avg_increase1,
       ff.old_day_avg1,
       ff.old_day_avg11,
       ff.old_day_avg12,
       ff.old_day_avg2,
       ff.old_day_avg_increase2,
       dense_rank() over(order by day_avg_sale desc) as day_top
from view_bs_c_CUSTOMER cu,
(select c_customer_id,
        tot_amt_actual,
        tot_retail_amt_actual,
        tot_retail_qty,
        day_store_avg_sum,
        day_recharge_avg_sum,
        store_count,
        store_retail_count,
        store_recharge_count,
       round(decode(store_count,0,0,tot_amt_actual/store_count)) day_avg_sale,
       round(decode(store_retail_count, 0, 0, day_store_avg_sum / store_retail_count)) day_retail_avg_sale,
       round(decode(store_recharge_count, 0, 0, day_recharge_avg_sum / store_retail_count)) day_recharge_avg_sale,
       round(decode(store_count,0,0,qty/store_count)) day_avg_sum,
       round(decode(qty,0,0,tot_amt_actual/qty)) day_avg_price,
       round(decode(tot_retail_qty, 0, 0, tot_retail_amt_actual / tot_retail_qty)) day_avg_price1
from
  (select s.c_customer_id,sum(s.tot_amt_actual) tot_amt_actual,
 sum(case when (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0 then s.tot_amt_actual end) tot_retail_amt_actual,
          sum(case when s.m_product_name not like ''ZS%'' then s.tot_amt_actual end) day_store_avg_sum,
          nvl(sum(case when s.m_product_name like ''ZS%'' then s.tot_amt_actual end),0) day_recharge_avg_sum,
         sum(s.qty) tot_qty,
    sum(case when (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0 then s.qty end) tot_retail_qty,
         sum(case when s.m_product_name not like ''ZS%'' then s.qty end) qty,
count(distinct(s.c_store_id)) store_count,
   count(distinct case when s.m_product_name not like ''ZS%'' then s.c_store_id end) store_retail_count,
   count(distinct case when s.m_product_name like ''ZS%'' then s.c_store_id end) store_recharge_count
    from LS_BILL_D_RETAIL s,
         v_bs_c_store t,
         t_day d,
        (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
           from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
          where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)spsx
   where 1=1
     and s.c_store_id = t.id
     and s.billdate=d.id
     and s.m_product_id=spsx.id
 #   and d.t_day_day = to_date([billdate],''yyyymmdd'') #
     and t.business <> ''加盟店''
     and t.name not like ''%仓%''
     and t.name not like ''%特卖%''
      and t.name not like ''%福利%''
     and spsx.M_DIM18_ID in (240,241)
   group by s.c_customer_id))dd,

 (select c_customer_id,
         tot_amt_actual,
         qty,
         round(decode(qty, 0, 0, tot_amt_actual / qty)) day_avg_price
    from (select s.c_customer_id,
                 sum(s.tot_amt_actual) tot_amt_actual,
                 sum(s.qty) qty
            from LS_BILL_D_RETAIL s,v_bs_c_store t,t_day d,
                 (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
                    from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
                   where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
           where 1 = 1
             and s.c_store_id = t.id
             and s.billdate = d.id
             and s.m_product_id = spsx.id
             and d.t_day_day = to_date([billdate], ''yyyymmdd'')
             and t.business <> ''加盟店''
             and t.name not like ''%仓%''
             and t.name not like ''%特卖%''
             and t.name not like ''%福利%''
             and spsx.M_DIM18_ID in (240, 241)
             and not exists (select 1 from LS_BILL_D_RETAIL a where  a.m_product_name like ''ZS%'' and a.billdate=[billdate] and a.id=s.id )
           group by s.c_customer_id)) ee,

(select (case when a.c_customer_id is null then b.c_customer_id else a.c_customer_id end) c_customer_id,
       nvl(mon_store_sum1,0) mon_store_sum1,
       nvl(mon_store_sum11,0) mon_store_sum11,
       nvl(mon_store_sum12,0) mon_store_sum12,
       nvl(mon_store_sum2,0) mon_store_sum2,
       round(decode(nvl(mon_store_sum2,0),0,0,((nvl(mon_store_sum1,0)-nvl(mon_store_sum2,0))/nvl(mon_store_sum2,0))*100))||''%'' increase
from
(select c_customer_id,
        sum(tot_amt_actual_this) tot_amt_actual_this,
        sum(tot_amt_actual_this1) tot_amt_actual_this1,
        sum(tot_amt_actual_this2) tot_amt_actual_this2,
        sum(store_count_this) store_count_this,
        sum(store_count_this1) store_count_this1,
        sum(store_count_this2) store_count_this2,
        sum(sum_date) sum_date,
        sum(sum_date1) sum_date1,
        sum(sum_date2) sum_date2,
 #  round(decode(substr([billdate], -2),0,0,(decode(sum(store_count_this),0,0,sum(tot_amt_actual_this) / sum(sum_date))))) mon_store_sum1, #
 # round(decode(substr([billdate], -2),0,0,(decode(sum(store_count_this),0,0,sum(tot_amt_actual_this1) / sum(sum_date))))) mon_store_sum11, #
 # round(decode(substr([billdate], -2),0,0,(decode(sum(store_count_this),0,0,sum(tot_amt_actual_this2) / sum(sum_date))))) mon_store_sum12 #
  from
    (select m.c_customer_id,
            m.c_store_id,
           sum(m.tot_amt_actual) tot_amt_actual_this,
           sum(case when m.m_product_name not like ''ZS%'' then m.tot_amt_actual end) tot_amt_actual_this1,
           nvl(sum(case when m.m_product_name like ''ZS%'' then m.tot_amt_actual end),0) tot_amt_actual_this2,
           count(distinct m.c_store_id) store_count_this,
           count(case when m.m_product_name not like ''ZS%'' then m.c_store_id end) store_count_this1,
           count(case when m.m_product_name like ''ZS%'' then m.c_store_id end) store_count_this2,
           count(distinct m.billdate) sum_date,
           count(distinct case when m.m_product_name not like ''ZS%'' then m.billdate end) sum_date1,
           count(distinct case when m.m_product_name like ''ZS%'' then m.billdate end) sum_date2
    from LS_BILL_D_RETAIL m,
         v_bs_c_store t,
         (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
            from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
           where t.M_DIM2_ID=y.PRODUCT_YEAR_ID) spsx
    where 1=1
      and m.c_store_id = t.id
      and m.m_product_id=spsx.id
 #    and m.billdate between substr([billdate],0,6)||''01'' #
 #    and [billdate] #
      and t.business <> ''加盟店''
      and t.name not like ''%仓%''
      and t.name not like ''%特卖%''
      and t.name not like ''%福利%''
      and spsx.M_DIM18_ID in (240,241)
    group by m.c_customer_id,m.c_store_id
   having sum(m.tot_amt_actual)<>0)
    group by c_customer_id
)a
full outer join
(

select c_customer_id,
       sum(tot_amt_actual_this) tot_amt_actual_this,
       sum(tot_amt_actual_this1) tot_amt_actual_this1,
       sum(tot_amt_actual_this2) tot_amt_actual_this2,
       sum(store_count_this) store_count_this,
 # round(decode(substr(to_char(add_months(to_date([billdate],''yyyymmdd''),-24),''yyyymmdd''),-2),0,0,(decode(sum(store_count_this),0,0,sum(tot_amt_actual_this)/sum(sum_date))))) mon_store_sum2 #
  from
    (select m.c_customer_id,m.c_store_id,
           sum(m.tot_amt_actual) tot_amt_actual_this,
           sum(case when m.m_product_name not like ''ZS%'' then m.tot_amt_actual end) tot_amt_actual_this1,
           nvl(sum(case when m.m_product_name like ''ZS%'' then m.tot_amt_actual end),0) tot_amt_actual_this2,
           count(distinct m.c_store_id) store_count_this,
           count(distinct m.billdate) sum_date
    from LS_BILL_D_RETAIL m,
         v_bs_c_store t,
        (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
           from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
          where t.M_DIM2_ID=y.PRODUCT_YEAR_ID) spsx
    where 1=1
      and m.c_store_id = t.id
      and m.m_product_id=spsx.id
 # and m.billdate between substr(to_char(add_months(to_date([billdate],''yyyymmdd''),-24),''yyyymmdd''),0,6)||''01''  #
 #    and to_char(add_months(to_date([billdate],''yyyymmdd''),-24),''yyyymmdd'') #
      and t.business <> ''加盟店''
      and t.name not like ''%仓%''
      and t.name not like ''%特卖%''
      and t.name not like ''%福利%''
      and spsx.M_DIM18_ID in (240,241)
    group by m.c_customer_id,m.c_store_id
   having sum(m.tot_amt_actual)<>0)
    group by c_customer_id
)b
on a.c_customer_id=b.c_customer_id) mm,

(
select c_customer_id,
       round(avg(avg_new)) old_year_day_avg1,
       round(avg(avg_new1)) old_year_day_avg11,
       round(avg(avg_new2)) old_year_day_avg12,
       round(avg(avg_old)) old_year_day_avg2,
       round(decode(nvl(avg(avg_old),0),0,0,((nvl(avg(avg_new),0)-nvl(avg(avg_old),0))/nvl(avg(avg_old),0))*100))||''%'' old_day_avg_increase1
 from (
select a.c_customer_id,
       a.c_store_id,
       a.tot_amt_actual_this tot_amt_acutal_new,
       a.store_count_this store_count_new,
       a.sum_date sum_new,
       a.amt_actual_store amt_actual_new,
       round(a.tot_amt_actual_this/a.sum_date,2) avg_new,
       round(a.tot_amt_actual_this1/a.sum_date,2) avg_new1,
       round(a.tot_amt_actual_this2/a.sum_date,2) avg_new2,
       b.tot_amt_actual_this tot_amt_acutal_old,
       b.store_count_this store_count_old,
       b.sum_date sum_old,
       b.amt_actual_store amt_actual_old,
       round(b.tot_amt_actual_this/b.sum_date,2) avg_old
from (
select m.c_customer_id,
       m.c_store_id,
       sum(m.tot_amt_actual) tot_amt_actual_this,
       sum(case when m.m_product_name not like ''ZS%'' then m.tot_amt_actual end) tot_amt_actual_this1,
       nvl(sum(case when m.m_product_name like ''ZS%'' then m.tot_amt_actual end),0) tot_amt_actual_this2,
       count(distinct(m.c_store_id)) store_count_this,
       count(distinct m.billdate) sum_date,
       count(distinct case when m.m_product_name not like ''ZS%'' then m.billdate end) sum_date1,
       count(distinct case when m.m_product_name like ''ZS%'' then m.billdate end) sum_date2,
       round(decode(count(distinct m.billdate),0,0,sum(m.tot_amt_actual) / count(distinct m.billdate)),2) amt_actual_store
  from LS_BILL_D_RETAIL m,
       v_bs_c_store t,
       (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
          from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
         where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
 where 1 = 1
   and m.c_store_id = t.id
   and m.m_product_id = spsx.id
 # and m.billdate between substr([billdate], 0, 6)||''01'' #
 # and [billdate] #
 # and to_date([billdate], ''yyyymmdd'') - to_date(t.rentbegin, ''yyyymmdd'') > 365 #
   and t.business <> ''加盟店''
   and t.name not like ''%仓%''
   and t.name not like ''%特卖%''
   and t.name not like ''%福利%''
   and t.name not like ''%微信%''
   and spsx.M_DIM18_ID in (240, 241)
 group by m.c_customer_id, m.c_store_id
having sum(m.tot_amt_actual)<>0) a
inner join
(select m.c_customer_id,
       m.c_store_id,
       sum(m.tot_amt_actual) tot_amt_actual_this,
       count(distinct(m.c_store_id)) store_count_this,
       count(distinct m.billdate) sum_date,
       round(decode(count(distinct m.billdate),0,0,sum(m.tot_amt_actual) / count(distinct m.billdate)),2) amt_actual_store
  from LS_BILL_D_RETAIL m,
       v_bs_c_store t,
       (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
          from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
         where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
 where 1 = 1
   and m.c_store_id = t.id
   and m.m_product_id = spsx.id
 # and m.billdate between substr(to_char(add_months(to_date([billdate], ''yyyymmdd''), -24),''yyyymmdd''),0,6) || ''01'' #
 # and to_char(add_months(to_date([billdate], ''yyyymmdd''), -24), ''yyyymmdd'') #
 # and to_date([billdate], ''yyyymmdd'') - to_date(t.rentbegin, ''yyyymmdd'') > 365 #
   and t.business <> ''加盟店''
   and t.name not like ''%仓%''
   and t.name not like ''%特卖%''
   and t.name not like ''%福利%''
   and t.name not like ''%微信%''
   and spsx.M_DIM18_ID in (240, 241)
 group by m.c_customer_id, m.c_store_id
having sum(m.tot_amt_actual)<>0) b
 on a.c_customer_id=b.c_customer_id and a.c_store_id=b.c_store_id)
 group by c_customer_id)gg,
(select
a.c_customer_id,
round(avg(a.tot_amt_actual)) old_day_avg1,
round(avg(a.tot_amt_actual1)) old_day_avg11,
round(avg(a.tot_amt_actual2)) old_day_avg12,
round(avg(b.tot_amt_actual)) old_day_avg2,
round(decode(nvl(avg(b.tot_amt_actual),0),0,0,((nvl(avg(a.tot_amt_actual),0)-nvl(avg(b.tot_amt_actual),0))/nvl(avg(b.tot_amt_actual),0))*100))||''%'' old_day_avg_increase2
from (
select s.c_customer_id,
       s.c_store_id,
       sum(s.tot_amt_actual) tot_amt_actual,
       sum(case when s.m_product_name not like ''ZS%'' then s.tot_amt_actual end) tot_amt_actual1,
       nvl(sum(case when s.m_product_name like ''ZS%'' then s.tot_amt_actual end),0) tot_amt_actual2
  from LS_BILL_D_RETAIL s,
       v_bs_c_store t,
       (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
          from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
         where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
 where 1 = 1
   and s.c_store_id = t.id
   and s.m_product_id = spsx.id
 # and s.billdate = [billdate] #
 # and to_date([billdate], ''yyyymmdd'') - to_date(t.rentbegin, ''yyyymmdd'') > 365 #
   and t.business <> ''加盟店''
   and t.name not like ''%仓%''
   and t.name not like ''%福利%''
   and t.name not like ''%特卖%''
   and spsx.M_DIM18_ID in (240, 241)
 group by s.c_customer_id, s.c_store_id
having sum(s.tot_amt_actual)<>0) a
inner join
(
select s.c_customer_id,
       s.c_store_id,
       sum(s.tot_amt_actual) tot_amt_actual,
       sum(case when s.m_product_name not like ''ZS%'' then s.tot_amt_actual end) tot_amt_actual1,
       sum(case when s.m_product_name like ''ZS%'' then s.tot_amt_actual end) tot_amt_actual2
  from LS_BILL_D_RETAIL s,
       v_bs_c_store t,
       (select t.id,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.M_DIM18_ID
          from BIFX_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
         where t.M_DIM2_ID = y.PRODUCT_YEAR_ID) spsx
 where 1 = 1
   and s.c_store_id = t.id
   and s.m_product_id = spsx.id
 # and s.billdate =to_char(add_months(to_date([billdate], ''yyyymmdd''),-24), ''yyyymmdd'')  #
 # and to_date([billdate], ''yyyymmdd'') -to_date(t.rentbegin, ''yyyymmdd'') > 365 #
   and t.business <> ''加盟店''
   and t.name not like ''%仓%''
   and t.name not like ''%特卖%''
   and t.name not like ''%福利%''
   and spsx.M_DIM18_ID in (240, 241)
 group by s.c_customer_id,s.c_store_id
having sum(s.tot_amt_actual)<>0) b
 on a.c_customer_id=b.c_customer_id and a.c_store_id=b.c_store_id
 group by a.c_customer_id) ff
where cu.c_customer_id=dd.c_customer_id
  and dd.c_customer_id=ee.c_customer_id(+)
  and dd.c_customer_id=mm.c_customer_id(+)
  and dd.c_customer_id=gg.c_customer_id(+)
  and dd.c_customer_id=ff.c_customer_id(+)
order by day_avg_sale desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '零售占比', 'lszb', '10', '143', 'select decode(grouping(area),1,''全国汇总'',area) area,
       decode(grouping(customer),1,''区域汇总'',2,''全国合计'',customer) customer,
       round(avg(new_sales_share))||''%'' new_sales_share,
       round(avg(new_avg_price)) new_avg_price,
       round(avg(new_avg_price1)) new_avg_price1,
       round(avg(new_inventory_ratio))||''%'' new_inventory_ratio,
       round(avg(new_store_volume)) new_store_volume,
       round(avg(old_sales_share))||''%'' old_sales_share,
       round(avg(old_avg_price)) old_avg_price,
       round(avg(old_avg_price1)) old_avg_price1,
       round(avg(old_inventory_ratio))||''%'' old_inventory_ratio,
       round(avg(old_store_volume)) old_store_volume
from
(select area_name as area,
        c_customer_name customer,
        new_sales_share,
        new_avg_price,
        new_avg_price1,
        new_inventory_ratio,
        new_store_volume,
        old_sales_share,
        old_avg_price,
        old_avg_price1,
        old_inventory_ratio,
        old_store_volume
from
(select max(lls.area_name) area_name,max(lls.c_customer_name) c_customer_name,lls.c_customer_id,
 # round(sum(case when lls.billdate=to_char(to_date([billdate], ''yyyymmdd'') , ''YYYYMMDD'') then decode(qty,0,0,qty_season/qty) else 0 end)*100) new_sales_share, #
 # round(sum(case when lls.billdate=to_char(to_date([billdate], ''yyyymmdd'') , ''YYYYMMDD'') then decode(qty_season,0,0,tot_amt_actual_season/qty_season) else 0 end)) new_avg_price,  #
 # round(sum(case when lls.billdate=to_char(to_date([billdate], ''yyyymmdd'') , ''YYYYMMDD'') then decode(qty_season1,0,0,tot_amt_actual_season1/qty_season1) else 0 end)) new_avg_price1,  #
 # round(sum(case when lls.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then decode(qty,0,0,old_qty_season/qty) else 0 end)*100) old_sales_share,  #
 # round(sum(case when lls.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then decode(old_qty_season,0,0,old_tot_amt_actual_season/old_qty_season) else 0 end)) old_avg_price, #
 # round(sum(case when lls.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then decode(old_qty_season1,0,0,old_tot_amt_actual_season1/old_qty_season1) else 0 end)) old_avg_price1  #
from
(select max(ls.area_name) area_name,
        max(ls.name) c_customer_name,
        ls.c_customer_id,
        ls.billdate,
        sum(ls.qty) qty,
        sum(ls.tot_amt_actual) tot_amt_actual,
        sum(ls.qty_season) qty_season,
        sum(ls.qty_season1) qty_season1,
        sum(ls.tot_amt_actual_season) tot_amt_actual_season,
        sum(ls.tot_amt_actual_season1) tot_amt_actual_season1,
        sum(ls.old_qty_season) old_qty_season,
        sum(ls.old_qty_season1) old_qty_season1,
        sum(ls.old_tot_amt_actual_season) old_tot_amt_actual_season,
        sum(ls.old_tot_amt_actual_season1) old_tot_amt_actual_season1
from(select ca.area_name,ca.name,s.c_customer_id,s.billdate,s.qty,s.tot_amt_actual,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]  #
 #          and spss.M_DIM9_ID=[season] #
        then s.qty else 0 end) as qty_season,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]  #
 #          and spss.M_DIM9_ID=[season] #
 and (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0
        then s.qty else 0 end) as qty_season1,
 #        (case when spss.PRODUCT_YEAR_NAME=[year] #
 #         and spss.M_DIM9_ID=[season] #
        then s.tot_amt_actual else 0 end) as tot_amt_actual_season ,
 #        (case when spss.PRODUCT_YEAR_NAME=[year] #
 #         and spss.M_DIM9_ID=[season] #
 and (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0
        then s.tot_amt_actual else 0 end) as tot_amt_actual_season1,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
        then s.qty else 0 end) as old_qty_season,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
     and (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0
        then s.qty else 0 end) as old_qty_season1,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
        then s.tot_amt_actual else 0 end) as old_tot_amt_actual_season,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
     and (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0
        then s.tot_amt_actual else 0 end) as old_tot_amt_actual_season1
 from LS_BILL_D_RETAIL s
  left outer join v_bs_c_store t
    on s.c_store_id = t.id
  right outer join (select bs.id,bs.name,bs.area_name
                      from bs_customer_area bs
                     where exists (select 1
                                     from table(f_store_table(''@fdd''))fst
                                    where bs.id=fst.c_customer_id)) ca
    on s.c_customer_id=ca.id,
    (select t.id,t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
       from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
      where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)spss
 where 1 = 1
   and s.M_PRODUCT_ID=spss.id
 #    and (s.billdate =to_char(to_date([billdate], ''yyyymmdd''), ''YYYYMMDD'') #
 #    or s.billdate = to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'')) #
   and s.status=2
   and t.business <> ''加盟店'' and t.name not like ''%仓%'' and t.name not like ''%特卖%'' and t.name not like ''%福利%'')ls
       group by ls.c_customer_id,ls.billdate)lls
       group by lls.c_customer_id) xlzb,
(select kc.c_customer_id, round(decode(kc_all,0,0,kc_season/kc_all)*100) as new_inventory_ratio, round(decode(pdsCstore,0,0,kc_season/pdsCstore)) as new_store_volume,
              round(decode(old_kc_all,0,0,old_kc_season/old_kc_all)*100) as old_inventory_ratio, round(decode(old_store_count,0,0,old_kc_season/old_store_count)) as old_store_volume
from
(select b.c_customer_id,
 #        sum(case when b.billdate=to_char(to_date([billdate], ''yyyymmdd''),''YYYYMMDD'') then kc2 else 0 end) kc_all, #
 #        sum(case when b.billdate=to_char(to_date([billdate], ''yyyymmdd''),''YYYYMMDD'') #
 #             and spss.PRODUCT_YEAR_NAME=[year]  #
 #             and spss.M_DIM9_ID=[season] #
           then b.kc2 else 0 end) kc_season,
 #        sum(case when b.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then kc2 else 0 end) old_kc_all, #
 #        sum(case when b.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') #
 #         and spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
           then b.kc2 else 0 end) old_kc_season
from BI_FA_STORAGE_xsfx  b,
     (select bs.id,bs.name,bs.area_name
        from bs_customer_area bs
       where exists (select 1
                       from table(f_store_table(''@fdd''))fst
                      where bs.id=fst.c_customer_id)) ca,
     (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
       from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
      where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
      group by  t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID) spss
where b.e_name=spss.e_name
     and b.c_customer_id=ca.id
 #   and (b.billdate=to_char(to_date([billdate], ''yyyymmdd''), ''YYYYMMDD'') #
 #       or b.billdate = to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'')) #
group by b.c_customer_id)  kc,
(select pds.c_customer_id,
        count(distinct pds.c_store_id) pdsCstore
   from pds_store pds,v_bs_c_store c,
        (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
           from BI_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
          where t.M_DIM2_ID = y.PRODUCT_YEAR_ID
          group by t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID) spss
  where pds.e_name=spss.e_name
    and pds.c_store_id=c.id
    and c.business_id in (7,8)
    and type_id=1
 #  and PRODUCT_YEAR_NAME =[year]  #
 #  and M_DIM9_ID =[season] #
  group by pds.c_customer_id) pds,
 # (select d.c_customer_id,count(distinct(case when d.billdate=to_char(to_date([billdate], ''yyyymmdd''), ''YYYYMMDD'') then d.c_store_id end)) store_count, #
 #                         count(distinct(case when d.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then d.c_store_id end)) old_store_count #
from ls_store_d_retail d
left outer join v_bs_c_store s
            on d.c_store_id = s.id
 # where (d.billdate =to_char(to_date([billdate], ''yyyymmdd''), ''YYYYMMDD'') #
 #    or d.billdate = to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'')) #
           and s.business <> ''加盟店''
   and s.name not like ''%仓%''
   and s.name not like ''%测试%''
   and s.name not like ''%直播%''
   and s.name not like ''%微信%''
   and s.name not like ''%福利%''
   and s.name not like ''%特卖%''
group by d.c_customer_id) stcn
where kc.c_customer_id=stcn.c_customer_id
  and pds.c_customer_id=kc.c_customer_id) kczb
where xlzb.c_customer_id=kczb.c_customer_id(+))
group by rollup(area,customer)', 'select decode(grouping(area),1,''全国汇总'',area) area,
       decode(grouping(customer),1,''区域汇总'',2,''全国合计'',customer) customer,
       round(avg(new_sales_share))||''%'' new_sales_share,
       round(avg(new_avg_price)) new_avg_price,
       round(avg(new_avg_price1)) new_avg_price1,
       round(avg(new_inventory_ratio))||''%'' new_inventory_ratio,
       round(avg(new_store_volume)) new_store_volume,
       round(avg(old_sales_share))||''%'' old_sales_share,
       round(avg(old_avg_price)) old_avg_price,
       round(avg(old_avg_price1)) old_avg_price1,
       round(avg(old_inventory_ratio))||''%'' old_inventory_ratio,
       round(avg(old_store_volume)) old_store_volume
from
(select area_name as area,
        c_customer_name customer,
        new_sales_share,
        new_avg_price,
        new_avg_price1,
        new_inventory_ratio,
        new_store_volume,
        old_sales_share,
        old_avg_price,
        old_avg_price1,
        old_inventory_ratio,
        old_store_volume
from
(select max(lls.area_name) area_name,max(lls.c_customer_name) c_customer_name,lls.c_customer_id,
 # round(sum(case when lls.billdate=to_char(to_date([billdate], ''yyyymmdd'') , ''YYYYMMDD'') then decode(qty,0,0,qty_season/qty) else 0 end)*100) new_sales_share, #
 # round(sum(case when lls.billdate=to_char(to_date([billdate], ''yyyymmdd'') , ''YYYYMMDD'') then decode(qty_season,0,0,tot_amt_actual_season/qty_season) else 0 end)) new_avg_price,  #
 # round(sum(case when lls.billdate=to_char(to_date([billdate], ''yyyymmdd'') , ''YYYYMMDD'') then decode(qty_season1,0,0,tot_amt_actual_season1/qty_season1) else 0 end)) new_avg_price1,  #
 # round(sum(case when lls.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then decode(qty,0,0,old_qty_season/qty) else 0 end)*100) old_sales_share,  #
 # round(sum(case when lls.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then decode(old_qty_season,0,0,old_tot_amt_actual_season/old_qty_season) else 0 end)) old_avg_price, #
 # round(sum(case when lls.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then decode(old_qty_season1,0,0,old_tot_amt_actual_season1/old_qty_season1) else 0 end)) old_avg_price1  #
from
(select max(ls.area_name) area_name,
        max(ls.name) c_customer_name,
        ls.c_customer_id,
        ls.billdate,
        sum(ls.qty) qty,
        sum(ls.tot_amt_actual) tot_amt_actual,
        sum(ls.qty_season) qty_season,
        sum(ls.qty_season1) qty_season1,
        sum(ls.tot_amt_actual_season) tot_amt_actual_season,
        sum(ls.tot_amt_actual_season1) tot_amt_actual_season1,
        sum(ls.old_qty_season) old_qty_season,
        sum(ls.old_qty_season1) old_qty_season1,
        sum(ls.old_tot_amt_actual_season) old_tot_amt_actual_season,
        sum(ls.old_tot_amt_actual_season1) old_tot_amt_actual_season1
from(select ca.area_name,ca.name,s.c_customer_id,s.billdate,s.qty,s.tot_amt_actual,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]  #
 #          and spss.M_DIM9_ID=[season] #
        then s.qty else 0 end) as qty_season,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]  #
 #          and spss.M_DIM9_ID=[season] #
 and (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0
        then s.qty else 0 end) as qty_season1,
 #        (case when spss.PRODUCT_YEAR_NAME=[year] #
 #         and spss.M_DIM9_ID=[season] #
        then s.tot_amt_actual else 0 end) as tot_amt_actual_season ,
 #        (case when spss.PRODUCT_YEAR_NAME=[year] #
 #         and spss.M_DIM9_ID=[season] #
 and (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0
        then s.tot_amt_actual else 0 end) as tot_amt_actual_season1,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
        then s.qty else 0 end) as old_qty_season,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
     and (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0
        then s.qty else 0 end) as old_qty_season1,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
        then s.tot_amt_actual else 0 end) as old_tot_amt_actual_season,
 #        (case when spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
     and (select count(1) from ls_bill_d_retail a where a.docno=s.docno and a.m_product_name like ''ZS%'')=0
        then s.tot_amt_actual else 0 end) as old_tot_amt_actual_season1
 from LS_BILL_D_RETAIL s
  left outer join v_bs_c_store t
    on s.c_store_id = t.id
  right outer join bs_customer_area ca
    on s.c_customer_id=ca.id,
    (select t.id,t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
       from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
      where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)spss
 where 1 = 1
   and s.M_PRODUCT_ID=spss.id
 #    and (s.billdate =to_char(to_date([billdate], ''yyyymmdd''), ''YYYYMMDD'') #
 #    or s.billdate = to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'')) #
   and s.status=2
   and t.business <> ''加盟店'' and t.name not like ''%仓%'' and t.name not like ''%特卖%'' and t.name not like ''%福利%'')ls
       group by ls.c_customer_id,ls.billdate)lls
       group by lls.c_customer_id) xlzb,
(select kc.c_customer_id, round(decode(kc_all,0,0,kc_season/kc_all)*100) as new_inventory_ratio, round(decode(pdsCstore,0,0,kc_season/pdsCstore)) as new_store_volume,
              round(decode(old_kc_all,0,0,old_kc_season/old_kc_all)*100) as old_inventory_ratio, round(decode(old_store_count,0,0,old_kc_season/old_store_count)) as old_store_volume
from
(select b.c_customer_id,
 #        sum(case when b.billdate=to_char(to_date([billdate], ''yyyymmdd''),''YYYYMMDD'') then kc2 else 0 end) kc_all, #
 #        sum(case when b.billdate=to_char(to_date([billdate], ''yyyymmdd''),''YYYYMMDD'') #
 #             and spss.PRODUCT_YEAR_NAME=[year]  #
 #             and spss.M_DIM9_ID=[season] #
           then b.kc2 else 0 end) kc_season,
 #        sum(case when b.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then kc2 else 0 end) old_kc_all, #
 #        sum(case when b.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') #
 #         and spss.PRODUCT_YEAR_NAME=[year]-1  #
 #         and spss.M_DIM9_ID=[season] #
           then b.kc2 else 0 end) old_kc_season
from BI_FA_STORAGE_xsfx  b,
     bs_customer_area ca,
     (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
       from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
      where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
      group by  t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID) spss
where b.e_name=spss.e_name
     and b.c_customer_id=ca.id
 #   and (b.billdate=to_char(to_date([billdate], ''yyyymmdd''), ''YYYYMMDD'') #
 #       or b.billdate = to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'')) #
group by b.c_customer_id)  kc,
(select pds.c_customer_id,
        count(distinct pds.c_store_id) pdsCstore
   from pds_store pds,v_bs_c_store c,
        (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
           from BI_M_PRODUCT t, VIEW_DIC_PRODUCT_YEAR y
          where t.M_DIM2_ID = y.PRODUCT_YEAR_ID
          group by t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID) spss
  where pds.e_name=spss.e_name
    and pds.c_store_id=c.id
    and c.business_id in (7,8)
    and type_id=1
 #  and PRODUCT_YEAR_NAME =[year]  #
 #  and M_DIM9_ID =[season] #
  group by pds.c_customer_id) pds,
 # (select d.c_customer_id,count(distinct(case when d.billdate=to_char(to_date([billdate], ''yyyymmdd''), ''YYYYMMDD'') then d.c_store_id end)) store_count, #
 #                         count(distinct(case when d.billdate=to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'') then d.c_store_id end)) old_store_count #
from ls_store_d_retail d
left outer join v_bs_c_store s
            on d.c_store_id = s.id
 # where (d.billdate =to_char(to_date([billdate], ''yyyymmdd''), ''YYYYMMDD'') #
 #    or d.billdate = to_char(add_months(to_date([billdate],''yyyymmdd''),-12),''yyyymmdd'')) #
           and s.business <> ''加盟店''
   and s.name not like ''%仓%''
   and s.name not like ''%测试%''
   and s.name not like ''%直播%''
   and s.name not like ''%微信%''
   and s.name not like ''%福利%''
   and s.name not like ''%特卖%''
group by d.c_customer_id) stcn
where kc.c_customer_id=stcn.c_customer_id
  and pds.c_customer_id=kc.c_customer_id) kczb
where xlzb.c_customer_id=kczb.c_customer_id(+))
group by rollup(area,customer)', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '版面结构分析', 'bmjgfx', '10', '144', 'select decode(grouping(customer),1,substr(customer,0,2)||''汇总'',2,substr(customer,0,2)||''汇总'',customer) customer,decode(grouping(mid6),1,substr(customer,0,2)||''汇总'',mid6) mid6,
       sum(product) product,sum(sku) sku,sum(whole_sale_customer) whole_sale_customer,sum(cumulative_sales) cumulative_sales,
       sum(last_week) last_week,sum(this_week) this_week,
       round(avg(quantity_loop_ratio))||''%'' quantity_loop_ratio,
       round(avg(cumulative_gross_margin))||''%'' cumulative_gross_margin,
       round(avg(avg_cost)) avg_cost,
       round(avg(all_unit_price)) all_unit_price,
       round(avg(last_week_unit_price)) last_week_unit_price,
       round(avg(this_week_unit_price)) this_week_unit_price,
       round(avg(last_week_unit_price))-round(avg(this_week_unit_price)) unit_price_change,
       sum(storage_num) storage_num,
       sum(store_num) store_num,
       round(avg(sold_out_rate))||''%'' sold_out_rate,
       round(avg(week_storage_sale)) week_storage_sale
from
(select cm.name customer,dim28.attribname mid6,ls.e_name product,ls.m_product_id sku,pf.pf whole_sale_customer,
       ls.lsl cumulative_sales,ls.last_week_lsl last_week,ls.this_week_lsl this_week,
       round(decode(last_week_lsl,0,0,(this_week_lsl-last_week_lsl)/last_week_lsl)*100) quantity_loop_ratio,
       round(decode(ls.tot_amt_actual,0,0,(ls.tot_amt_actual-ls.ljcb)/ls.tot_amt_actual)*100) cumulative_gross_margin,
       round(decode(ls.lsl,0,0,ls.ljcb/ls.lsl)) avg_cost,
       round(decode(ls.lsl,0,0,ls.tot_amt_actual/ls.lsl)) all_unit_price,
       round(decode(ls.last_week_lsl,0,0,ls.last_week_tot_amt_actual/ls.last_week_lsl)) last_week_unit_price,
       round(decode(ls.this_week_lsl,0,0,ls.this_week_tot_amt_actual/ls.this_week_lsl)) this_week_unit_price,
       round(decode(ls.this_week_lsl,0,0,ls.this_week_tot_amt_actual/ls.this_week_lsl))-round(decode(ls.last_week_lsl,0,0,ls.last_week_tot_amt_actual/ls.last_week_lsl)) unit_price_change,
       kc.ccl storage_num,
       kc.dcl store_num,
       round(decode(pf.pf+ls.lsl+kc.ccl+kc.dcl,0,0,(pf.pf+ls.lsl)/(pf.pf+ls.lsl+kc.ccl+kc.dcl))*100) sold_out_rate,
       round(decode(ls.last_week_lsl+ls.this_week_lsl,0,0,kc.dcl/((ls.last_week_lsl+ls.this_week_lsl)/2))) week_storage_sale
from

(select ca.id,ca.name,ca.area_name
  from bs_customer_area ca) cm,

(select ca.name,tmsx.M_DIM28_ID,count(distinct(tmsx.e_name)) e_name,count(distinct(s.m_product_id)) m_product_id,sum(s.qty) lsl,sum(s.tot_amt_actual) tot_amt_actual,sum(s.qty*tmsx.SALEPRICE) ljcb,
       sum(case when s.billdate between to_char(sysdate-14,''yyyymmdd'') and to_char(sysdate-8,''yyyymmdd'') then qty else 0 end) last_week_lsl,
       sum(case when s.billdate between to_char(sysdate-14,''yyyymmdd'') and to_char(sysdate-8,''yyyymmdd'') then tot_amt_actual else 0 end) last_week_tot_amt_actual,
       sum(case when s.billdate between to_char(sysdate-7,''yyyymmdd'') and to_char(sysdate-1,''yyyymmdd'') then qty else 0 end) this_week_lsl,
       sum(case when s.billdate between to_char(sysdate-7,''yyyymmdd'') and to_char(sysdate-1,''yyyymmdd'') then tot_amt_actual else 0 end) this_week_tot_amt_actual
from LS_BILL_D_RETAIL s
right outer join bs_customer_area ca
on s.c_customer_id=ca.id,
(select t.id,t.e_name,t.M_DIM28_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.saleprice
        from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)tmsx
where s.m_product_id=tmsx.id
  and tmsx.PRODUCT_YEAR_NAME=@year
  and tmsx.M_DIM9_ID=@season
group by ca.name,tmsx.M_DIM28_ID
order by ca.name)ls,

(select m.id,m.attribname
from M_DIM@portal80 m
where m.m_dimdef_id=83)dim28,

(select ca.name,tmsx.M_DIM28_ID,sum(pf) pf
   from bi_m_saleitem_xsfx s
  right outer join bs_customer_area ca
     on s.c_customerup_id=ca.id,
(select t.e_name,t.M_DIM28_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
        from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
        group by t.e_name,t.M_DIM28_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID)tmsx
where s.e_name=tmsx.e_name
  and tmsx.PRODUCT_YEAR_NAME=@year
  and tmsx.M_DIM9_ID=@season
group by ca.name,tmsx.M_DIM28_ID
order by ca.name)pf,

(select ca.name,tmsx.M_DIM28_ID,sum(case when st.type=''仓库'' then w.qty else 0 end) ccl,
       sum(case when st.type=''店铺'' then w.qty else 0 end) dcl,
       count(distinct(case when st.business <> ''加盟店'' and st.name not like ''%仓%'' and st.name not like ''%特卖%'' then st.name end)) pds
  from BI_FA_STORAGE w
  left outer join v_bs_c_store st
  on w.c_store_id=st.id
  right outer join bs_customer_area ca
  on st.c_customer_id=ca.id,
  (select t.id,t.e_name,t.M_DIM28_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.saleprice
        from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)tmsx
  where w.m_product_id=tmsx.id
    and tmsx.PRODUCT_YEAR_NAME=@year
    and tmsx.M_DIM9_ID=@season
  group by ca.name,tmsx.M_DIM28_ID
  order by ca.name)kc

where cm.name=ls.name
  and ls.name=pf.name
  and ls.name=kc.name
  and ls.M_DIM28_ID=pf.M_DIM28_ID
  and ls.M_DIM28_ID=kc.M_DIM28_ID
  and ls.M_DIM28_ID=dim28.id)
group by rollup(customer,mid6)', 'select decode(grouping(customer),1,substr(customer,0,2)||''汇总'',2,substr(customer,0,2)||''汇总'',customer) customer,decode(grouping(mid6),1,substr(customer,0,2)||''汇总'',mid6) mid6,
       sum(product) product,sum(sku) sku,sum(whole_sale_customer) whole_sale_customer,sum(cumulative_sales) cumulative_sales,
       sum(last_week) last_week,sum(this_week) this_week,
       round(avg(quantity_loop_ratio))||''%'' quantity_loop_ratio,
       round(avg(cumulative_gross_margin))||''%'' cumulative_gross_margin,
       round(avg(avg_cost)) avg_cost,
       round(avg(all_unit_price)) all_unit_price,
       round(avg(last_week_unit_price)) last_week_unit_price,
       round(avg(this_week_unit_price)) this_week_unit_price,
       round(avg(last_week_unit_price))-round(avg(this_week_unit_price)) unit_price_change,
       sum(storage_num) storage_num,
       sum(store_num) store_num,
       round(avg(sold_out_rate))||''%'' sold_out_rate,
       round(avg(week_storage_sale)) week_storage_sale
from
(select cm.name customer,dim28.attribname mid6,ls.e_name product,ls.m_product_id sku,pf.pf whole_sale_customer,
       ls.lsl cumulative_sales,ls.last_week_lsl last_week,ls.this_week_lsl this_week,
       round(decode(last_week_lsl,0,0,(this_week_lsl-last_week_lsl)/last_week_lsl)*100) quantity_loop_ratio,
       round(decode(ls.tot_amt_actual,0,0,(ls.tot_amt_actual-ls.ljcb)/ls.tot_amt_actual)*100) cumulative_gross_margin,
       round(decode(ls.lsl,0,0,ls.ljcb/ls.lsl)) avg_cost,
       round(decode(ls.lsl,0,0,ls.tot_amt_actual/ls.lsl)) all_unit_price,
       round(decode(ls.last_week_lsl,0,0,ls.last_week_tot_amt_actual/ls.last_week_lsl)) last_week_unit_price,
       round(decode(ls.this_week_lsl,0,0,ls.this_week_tot_amt_actual/ls.this_week_lsl)) this_week_unit_price,
       round(decode(ls.this_week_lsl,0,0,ls.this_week_tot_amt_actual/ls.this_week_lsl))-round(decode(ls.last_week_lsl,0,0,ls.last_week_tot_amt_actual/ls.last_week_lsl)) unit_price_change,
       kc.ccl storage_num,
       kc.dcl store_num,
       round(decode(pf.pf+ls.lsl+kc.ccl+kc.dcl,0,0,(pf.pf+ls.lsl)/(pf.pf+ls.lsl+kc.ccl+kc.dcl))*100) sold_out_rate,
       round(decode(ls.last_week_lsl+ls.this_week_lsl,0,0,kc.dcl/((ls.last_week_lsl+ls.this_week_lsl)/2))) week_storage_sale
from

(select ca.id,ca.name,ca.area_name
  from bs_customer_area ca) cm,

(select ca.name,tmsx.M_DIM28_ID,count(distinct(tmsx.e_name)) e_name,count(distinct(s.m_product_id)) m_product_id,sum(s.qty) lsl,sum(s.tot_amt_actual) tot_amt_actual,sum(s.qty*tmsx.SALEPRICE) ljcb,
       sum(case when s.billdate between to_char(sysdate-14,''yyyymmdd'') and to_char(sysdate-8,''yyyymmdd'') then qty else 0 end) last_week_lsl,
       sum(case when s.billdate between to_char(sysdate-14,''yyyymmdd'') and to_char(sysdate-8,''yyyymmdd'') then tot_amt_actual else 0 end) last_week_tot_amt_actual,
       sum(case when s.billdate between to_char(sysdate-7,''yyyymmdd'') and to_char(sysdate-1,''yyyymmdd'') then qty else 0 end) this_week_lsl,
       sum(case when s.billdate between to_char(sysdate-7,''yyyymmdd'') and to_char(sysdate-1,''yyyymmdd'') then tot_amt_actual else 0 end) this_week_tot_amt_actual
from LS_BILL_D_RETAIL s
right outer join bs_customer_area ca
on s.c_customer_id=ca.id,
(select t.id,t.e_name,t.M_DIM28_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.saleprice
        from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)tmsx
where s.m_product_id=tmsx.id
  and tmsx.PRODUCT_YEAR_NAME=@year
  and tmsx.M_DIM9_ID=@season
group by ca.name,tmsx.M_DIM28_ID
order by ca.name)ls,

(select m.id,m.attribname
from M_DIM@portal80 m
where m.m_dimdef_id=83)dim28,

(select ca.name,tmsx.M_DIM28_ID,sum(pf) pf
   from bi_m_saleitem_xsfx s
  right outer join bs_customer_area ca
     on s.c_customerup_id=ca.id,
(select t.e_name,t.M_DIM28_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
        from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
        group by t.e_name,t.M_DIM28_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID)tmsx
where s.e_name=tmsx.e_name
  and tmsx.PRODUCT_YEAR_NAME=@year
  and tmsx.M_DIM9_ID=@season
group by ca.name,tmsx.M_DIM28_ID
order by ca.name)pf,

(select ca.name,tmsx.M_DIM28_ID,sum(case when st.type=''仓库'' then w.qty else 0 end) ccl,
       sum(case when st.type=''店铺'' then w.qty else 0 end) dcl,
       count(distinct(case when st.business <> ''加盟店'' and st.name not like ''%仓%'' and st.name not like ''%特卖%'' then st.name end)) pds
  from BI_FA_STORAGE w
  left outer join v_bs_c_store st
  on w.c_store_id=st.id
  right outer join bs_customer_area ca
  on st.c_customer_id=ca.id,
  (select t.id,t.e_name,t.M_DIM28_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.saleprice
        from BIFX_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)tmsx
  where w.m_product_id=tmsx.id
    and tmsx.PRODUCT_YEAR_NAME=@year
    and tmsx.M_DIM9_ID=@season
  group by ca.name,tmsx.M_DIM28_ID
  order by ca.name)kc

where cm.name=ls.name
  and ls.name=pf.name
  and ls.name=kc.name
  and ls.M_DIM28_ID=pf.M_DIM28_ID
  and ls.M_DIM28_ID=kc.M_DIM28_ID
  and ls.M_DIM28_ID=dim28.id)
group by rollup(customer,mid6)', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '进销存分析', 'jxcfx', '10', '145', 'select decode(grouping(area),1,''全国汇总'',area) area,decode(grouping(customer),1,''区域汇总'',2,''全国合计'',customer) customer,
       sum(out_goods_count) out_goods_count,sum(surplus_goods_count) surplus_goods_count,sum(wholesale_count) wholesale_count,
       sum(retail_count) retail_count,round(avg(unit_price)) unit_price,round(avg(whole_sale_rate))||''%'' whole_sale_rate,
       round(avg(gross_margin_rate))||''%'' gross_margin_rate,sum(storage1) storage1,sum(all_storage) all_storage,sum(store_num) store_num,
       round(avg(store_storage)) store_storage
from
(select area_name area,name customer,nvl(fhl,0) out_goods_count,nvl(dd-fhl,0) surplus_goods_count,nvl(pfl,0) wholesale_count,nvl(lssl,0) retail_count,nvl(ztdj,0) unit_price,nvl(round(decode(pfl+lssl+kcl,0,0,(pfl+lssl)/(pfl+lssl+kcl)*100)),0) whole_sale_rate,nvl(mll,0) gross_margin_rate,nvl(kcl,0) all_storage,nvl(pds,0) store_num,nvl(round(decode(pds,0,0,kcl/pds)),0) store_storage,nvl(ccl,0) storage1
 from (
select cm.area_name,cm.name,fh.fhl,pf.pfl,dd.dd,ls.qty lssl,round(decode(ls.qty,0,0,ls.tot_amt_actual/ls.qty)) ztdj,
           round((decode(ls.tot_amt_actual,0,0,(ls.tot_amt_actual-ls.ljcb)/ls.tot_amt_actual)*100)) mll,kc.ccl,kc.kcl,kc.pds
    from
    (select ca.id,ca.name,ca.area_name
    from bs_customer_area ca) cm,
    (select ca.name,sum(s.fh) fhl
    from bi_m_saleitem_xsfx s
    right outer join bs_customer_area ca
    on s.c_customer_id=ca.id,
    (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
        group by  t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID)spsx
    where s.e_name=spsx.e_name
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  fh,
    (select ca.name,sum(s.pf) pfl
    from bi_m_saleitem_xsfx s
    right outer join bs_customer_area ca
    on s.c_customerup_id=ca.id,
    (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
        group by  t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID)spsx
    where s.e_name=spsx.e_name
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  pf,
    (select ca.name,sum(b.dd) dd
    from bi_b_soitem_xsfx b
    right outer join bs_customer_area ca
    on b.c_customer_id=ca.id,
    (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
        group by  t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID)spsx
    where b.e_name=spsx.e_name
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  dd,
    (select ca.name,sum(d.tot_amt_actual) tot_amt_actual,sum(d.qty) qty,sum(d.qty*spsx.SALEPRICE) ljcb
    from LS_BILL_D_RETAIL d
    right outer join bs_customer_area ca
    on d.c_customer_id=ca.id,
    (select t.name,t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.SALEPRICE
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)spsx
    where d.m_product_name=spsx.name
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  ls,
    (select ca.name,sum(w.qty) kcl,sum(case when st.type=''仓库'' then w.qty else 0 end) ccl,
            count(distinct(case when st.business <> ''加盟店'' and st.name not like ''%仓%'' and st.name not like ''%特卖%'' then st.name end)) pds
    from BI_FA_STORAGE w
    left outer join v_bs_c_store st
    on w.c_store_id=st.id
    right outer join bs_customer_area ca
    on st.c_customer_id=ca.id,
    (select t.id,t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)spsx
    where w.m_product_id=spsx.id
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  kc
    where cm.name=fh.name(+)
      and cm.name=pf.name(+)
      and cm.name=dd.name(+)
      and cm.name=ls.name(+)
      and cm.name=kc.name(+)))
group by rollup(area,customer)', 'select decode(grouping(area),1,''全国汇总'',area) area,decode(grouping(customer),1,''区域汇总'',2,''全国合计'',customer) customer,
       sum(out_goods_count) out_goods_count,sum(surplus_goods_count) surplus_goods_count,sum(wholesale_count) wholesale_count,
       sum(retail_count) retail_count,round(avg(unit_price)) unit_price,round(avg(whole_sale_rate))||''%'' whole_sale_rate,
       round(avg(gross_margin_rate))||''%'' gross_margin_rate,sum(storage1) storage1,sum(all_storage) all_storage,sum(store_num) store_num,
       round(avg(store_storage)) store_storage
 from
(select area_name area,name customer,nvl(fhl,0) out_goods_count,nvl(dd-fhl,0) surplus_goods_count,nvl(pfl,0) wholesale_count,nvl(lssl,0) retail_count,nvl(ztdj,0) unit_price,nvl(round(decode(pfl+lssl+kcl,0,0,(pfl+lssl)/(pfl+lssl+kcl)*100)),0) whole_sale_rate,nvl(mll,0) gross_margin_rate,nvl(kcl,0) all_storage,nvl(pds,0) store_num,nvl(round(decode(pds,0,0,kcl/pds)),0) store_storage,nvl(ccl,0) storage1
 from (
select cm.area_name,cm.name,fh.fhl,pf.pfl,dd.dd,ls.qty lssl,round(decode(ls.qty,0,0,ls.tot_amt_actual/ls.qty)) ztdj,
           round((decode(ls.tot_amt_actual,0,0,(ls.tot_amt_actual-ls.ljcb)/ls.tot_amt_actual)*100)) mll,kc.ccl,kc.kcl,kc.pds
    from
    (select ca.id,ca.name,ca.area_name
    from bs_customer_area ca) cm,
    (select ca.name,sum(s.fh) fhl
    from bi_m_saleitem_xsfx s
    right outer join bs_customer_area ca
    on s.c_customer_id=ca.id,
    (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
        group by  t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID)spsx
    where s.e_name=spsx.e_name
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  fh,
    (select ca.name,sum(s.pf) pfl
    from bi_m_saleitem_xsfx s
    right outer join bs_customer_area ca
    on s.c_customerup_id=ca.id,
    (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
        group by  t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID)spsx
    where s.e_name=spsx.e_name
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  pf,
    (select ca.name,sum(b.dd) dd
    from bi_b_soitem_xsfx b
    right outer join bs_customer_area ca
    on b.c_customer_id=ca.id,
    (select t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID
        group by  t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID)spsx
    where b.e_name=spsx.e_name
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  dd,
    (select ca.name,sum(d.tot_amt_actual) tot_amt_actual,sum(d.qty) qty,sum(d.qty*spsx.SALEPRICE) ljcb
    from LS_BILL_D_RETAIL d
    right outer join bs_customer_area ca
    on d.c_customer_id=ca.id,
    (select t.name,t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID,t.SALEPRICE
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)spsx
    where d.m_product_name=spsx.name
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  ls,
    (select ca.name,sum(w.qty) kcl,sum(case when st.type=''仓库'' then w.qty else 0 end) ccl,
            count(distinct(case when st.business <> ''加盟店'' and st.name not like ''%仓%'' and st.name not like ''%特卖%'' then st.name end)) pds
    from BI_FA_STORAGE w
    left outer join v_bs_c_store st
    on w.c_store_id=st.id
    right outer join bs_customer_area ca
    on st.c_customer_id=ca.id,
    (select t.id,t.e_name,t.M_DIM2_ID,y.PRODUCT_YEAR_NAME,t.M_DIM9_ID
         from BI_M_PRODUCT t,VIEW_DIC_PRODUCT_YEAR y
        where t.M_DIM2_ID=y.PRODUCT_YEAR_ID)spsx
    where w.m_product_id=spsx.id
   #     and spsx.PRODUCT_YEAR_NAME=[year] #
   #     and spsx.M_DIM9_ID=[season] #
    group by ca.name)  kc
    where cm.name=fh.name(+)
      and cm.name=pf.name(+)
      and cm.name=dd.name(+)
      and cm.name=ls.name(+)
      and cm.name=kc.name(+)))
group by rollup(area,customer)', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '卡券使用情况1', 'kqsyqk1', '11', '146', 'select * from gic_coup_card_detail', 'select * from gic_coup_card_detail', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '卡券分析', 'kqfx', '11', '147', 'select ''郑州'' area,
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end) receive_open_store_name, #
 # (case when [costStoreName] is not null then a.cost_store_name end) cost_store_name, #
 # (case when [defaultStore] is not null then a.receive_open_store_name end) receive_open_store_name, #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number,
       max(a.clerk_id) clerk_id,max(a.clerk_name) clerk_name,
       count(a.card_id)card_count,
       sum(case when a.receive_card_num is not null then 1 else 0 end)receive_count,
       sum(case when a.use_card_num is not null then 1 else 0 end)use_count,
       sum(case when a.limit_time<now() and a.use_card_num is null then 1 else 0 end)limit_count,
       sum(case when a.receive_card_num is not null and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)no_use_amt,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=5 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_5,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=10 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_10,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=20 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_20,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=30 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_30,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=40 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_40,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=50 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_50,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=80 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_80,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=100 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_100,
       sum(case when a.card_type=''折扣券'' and a.use_card_num is null and a.limit_time>now() then 1 else 0 end)discount_count
from gemeiq.extract_coup_card_detail a
where a.card_name not like ''%测试%''
 and a.receive_open_store_name is not null
 # and a.erp_demo_code=[erpDemoCode] #
 # and a.receive_open_store_name like concat(''%'',[receiveOpenStoreName],''%'') #
 # and a.cost_store_name like concat(''%'',[costStoreName],''%'') #
 # and a.receive_phone_number like concat(''%'',[receivePhoneNumber],''%'') #
 # and a.use_phone_number like concat(''%'',[usePhoneNumber],''%'') #
 # and a.clerk_name=[clerkName] #
 # and [area]=''郑州'' #
group by
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end), #
 # (case when [costStoreName] is not null then a.cost_store_name end), #
 # (case when [defaultStore] is not null then a.receive_open_store_name end), #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number
union all

select ''南京'' area,
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end) receive_open_store_name, #
 # (case when [costStoreName] is not null then a.cost_store_name end) cost_store_name, #
 # (case when [defaultStore] is not null then a.receive_open_store_name end) receive_open_store_name, #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number,
       max(a.clerk_id) clerk_id,max(a.clerk_name) clerk_name,
       count(a.card_id)card_count,
       sum(case when a.receive_card_num is not null then 1 else 0 end)receive_count,
       sum(case when a.use_card_num is not null then 1 else 0 end)use_count,
       sum(case when a.limit_time<now() and a.use_card_num is null then 1 else 0 end)limit_count,
       sum(case when a.receive_card_num is not null and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)no_use_amt,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=5 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_5,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=10 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_10,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=20 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_20,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=30 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_30,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=40 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_40,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=50 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_50,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=80 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_80,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=100 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_100,
       sum(case when a.card_type=''折扣券'' and a.use_card_num is null and a.limit_time>now() then 1 else 0 end)discount_count
from gemeiqnj.extract_coup_card_detail a
where a.card_name not like ''%测试%''
 and a.receive_open_store_name is not null
 # and a.erp_demo_code=[erpDemoCode] #
 # and a.receive_open_store_name like concat(''%'',[receiveOpenStoreName],''%'') #
 # and a.cost_store_name like concat(''%'',[costStoreName],''%'') #
 # and a.receive_phone_number like concat(''%'',[receivePhoneNumber],''%'') #
 # and a.use_phone_number like concat(''%'',[usePhoneNumber],''%'') #
 # and a.clerk_name=[clerkName] #
 # and [area]=''南京'' #
group by
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end), #
 # (case when [costStoreName] is not null then a.cost_store_name end), #
 # (case when [defaultStore] is not null then a.receive_open_store_name end), #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number

union all

select ''南宁'' area,
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end) receive_open_store_name, #
 # (case when [costStoreName] is not null then a.cost_store_name end) cost_store_name, #
 # (case when [defaultStore] is not null then a.receive_open_store_name end) receive_open_store_name, #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number,
       max(a.clerk_id) clerk_id,max(a.clerk_name) clerk_name,
       count(a.card_id)card_count,
       sum(case when a.receive_card_num is not null then 1 else 0 end)receive_count,
       sum(case when a.use_card_num is not null then 1 else 0 end)use_count,
       sum(case when a.limit_time<now() and a.use_card_num is null then 1 else 0 end)limit_count,
       sum(case when a.receive_card_num is not null and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)no_use_amt,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=5 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_5,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=10 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_10,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=20 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_20,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=30 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_30,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=40 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_40,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=50 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_50,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=80 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_80,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=100 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_100,
       sum(case when a.card_type=''折扣券'' and a.use_card_num is null and a.limit_time>now() then 1 else 0 end)discount_count
from gemeiqnn.extract_coup_card_detail a
where a.card_name not like ''%测试%''
 and a.receive_open_store_name is not null
 # and a.erp_demo_code=[erpDemoCode] #
 # and a.receive_open_store_name like concat(''%'',[receiveOpenStoreName],''%'') #
 # and a.cost_store_name like concat(''%'',[costStoreName],''%'') #
 # and a.receive_phone_number like concat(''%'',[receivePhoneNumber],''%'') #
 # and a.use_phone_number like concat(''%'',[usePhoneNumber],''%'') #
 # and a.clerk_name=[clerkName] #
 # and [area]=''南宁'' #
group by
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end), #
 # (case when [costStoreName] is not null then a.cost_store_name end), #
 # (case when [defaultStore] is not null then a.receive_open_store_name end), #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number', 'select ''郑州'' area,
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end) receive_open_store_name, #
 # (case when [costStoreName] is not null then a.cost_store_name end) cost_store_name, #
 # (case when [defaultStore] is not null then a.receive_open_store_name end) receive_open_store_name, #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number,
       max(a.clerk_id) clerk_id,max(a.clerk_name) clerk_name,
       count(a.card_id)card_count,
       sum(case when a.receive_card_num is not null then 1 else 0 end)receive_count,
       sum(case when a.use_card_num is not null then 1 else 0 end)use_count,
       sum(case when a.limit_time<now() and a.use_card_num is null then 1 else 0 end)limit_count,
       sum(case when a.receive_card_num is not null and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)no_use_amt,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=5 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_5,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=10 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_10,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=20 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_20,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=30 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_30,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=40 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_40,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=50 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_50,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=80 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_80,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=100 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_100,
       sum(case when a.card_type=''折扣券'' and a.use_card_num is null and a.limit_time>now() then 1 else 0 end)discount_count
from gemeiq.extract_coup_card_detail a
where a.card_name not like ''%测试%''
 and a.receive_open_store_name is not null
 # and a.erp_demo_code=[erpDemoCode] #
 # and a.receive_open_store_name like concat(''%'',[receiveOpenStoreName],''%'') #
 # and a.cost_store_name like concat(''%'',[costStoreName],''%'') #
 # and a.receive_phone_number like concat(''%'',[receivePhoneNumber],''%'') #
 # and a.use_phone_number like concat(''%'',[usePhoneNumber],''%'') #
 # and a.clerk_name=[clerkName] #
 # and [area]=''郑州'' #
group by
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end), #
 # (case when [costStoreName] is not null then a.cost_store_name end), #
 # (case when [defaultStore] is not null then a.receive_open_store_name end), #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number
union all

select ''南京'' area,
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end) receive_open_store_name, #
 # (case when [costStoreName] is not null then a.cost_store_name end) cost_store_name, #
 # (case when [defaultStore] is not null then a.receive_open_store_name end) receive_open_store_name, #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number,
       max(a.clerk_id) clerk_id,max(a.clerk_name) clerk_name,
       count(a.card_id)card_count,
       sum(case when a.receive_card_num is not null then 1 else 0 end)receive_count,
       sum(case when a.use_card_num is not null then 1 else 0 end)use_count,
       sum(case when a.limit_time<now() and a.use_card_num is null then 1 else 0 end)limit_count,
       sum(case when a.receive_card_num is not null and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)no_use_amt,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=5 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_5,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=10 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_10,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=20 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_20,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=30 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_30,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=40 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_40,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=50 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_50,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=80 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_80,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=100 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_100,
       sum(case when a.card_type=''折扣券'' and a.use_card_num is null and a.limit_time>now() then 1 else 0 end)discount_count
from gemeiqnj.extract_coup_card_detail a
where a.card_name not like ''%测试%''
 and a.receive_open_store_name is not null
 # and a.erp_demo_code=[erpDemoCode] #
 # and a.receive_open_store_name like concat(''%'',[receiveOpenStoreName],''%'') #
 # and a.cost_store_name like concat(''%'',[costStoreName],''%'') #
 # and a.receive_phone_number like concat(''%'',[receivePhoneNumber],''%'') #
 # and a.use_phone_number like concat(''%'',[usePhoneNumber],''%'') #
 # and a.clerk_name=[clerkName] #
 # and [area]=''南京'' #
group by
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end), #
 # (case when [costStoreName] is not null then a.cost_store_name end), #
 # (case when [defaultStore] is not null then a.receive_open_store_name end), #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number

union all

select ''南宁'' area,
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end) receive_open_store_name, #
 # (case when [costStoreName] is not null then a.cost_store_name end) cost_store_name, #
 # (case when [defaultStore] is not null then a.receive_open_store_name end) receive_open_store_name, #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number,
       max(a.clerk_id) clerk_id,max(a.clerk_name) clerk_name,
       count(a.card_id)card_count,
       sum(case when a.receive_card_num is not null then 1 else 0 end)receive_count,
       sum(case when a.use_card_num is not null then 1 else 0 end)use_count,
       sum(case when a.limit_time<now() and a.use_card_num is null then 1 else 0 end)limit_count,
       sum(case when a.receive_card_num is not null and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)no_use_amt,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=5 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_5,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=10 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_10,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=20 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_20,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=30 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_30,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=40 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_40,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=50 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_50,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=80 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_80,
       sum(case when a.card_type=''抵金券'' and a.card_denomination=100 and a.use_card_num is null and a.limit_time>now() then a.card_denomination else 0 end)amt_100,
       sum(case when a.card_type=''折扣券'' and a.use_card_num is null and a.limit_time>now() then 1 else 0 end)discount_count
from gemeiqnn.extract_coup_card_detail a
where a.card_name not like ''%测试%''
 and a.receive_open_store_name is not null
 # and a.erp_demo_code=[erpDemoCode] #
 # and a.receive_open_store_name like concat(''%'',[receiveOpenStoreName],''%'') #
 # and a.cost_store_name like concat(''%'',[costStoreName],''%'') #
 # and a.receive_phone_number like concat(''%'',[receivePhoneNumber],''%'') #
 # and a.use_phone_number like concat(''%'',[usePhoneNumber],''%'') #
 # and a.clerk_name=[clerkName] #
 # and [area]=''南宁'' #
group by
 # (case when [receiveOpenStoreName] is not null then a.receive_open_store_name end), #
 # (case when [costStoreName] is not null then a.cost_store_name end), #
 # (case when [defaultStore] is not null then a.receive_open_store_name end), #
       a.receive_member_id,a.receive_card_num,a.receive_phone_number', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '新南昌零售分析', 'xnclsfx', '12', '148', 'with jxc as
(
select ls.m_dim2_id,ls.nf_name,ls.m_dim9_id,ls.jj_name,ls.dl_id,ls.dl_name,
       nvl(yh.yh,0)yh,nvl(kc.ck_store,0)ck_store,nvl(kc.dp_store,0)dp_store,
       nvl(dt.jxlsl,0)jxlsl,nvl(dt.jxlsje,0)jxlsje,nvl(dt.wzlsl,0)wzlsl,nvl(dt.wzlsje,0)wzlsje,
       nvl(pf.pf,0)pf,nvl(ls.ls,0)ls
from
 (
 select c.nf_name,c.jj_name,c.dl_name,nvl(sum(a.QTYREM),0)yh
 from b_soitem@Portal80 a,BS_C_STORE b,bi_m_product_xsfx_all c,BI_M_PRODUCT_all d,b_so@portal80 e
 where a.b_so_id=e.id
   and e.C_DEST_ID=b.id
   and a.m_product_id=d.id
   and d.e_name=c.e_name
 #    and e.billdate<=[billdate] #
   and b.c_customer_id in (251,214)
   and e.status=2
   and e.isactive=''Y''
 group by c.nf_name,c.jj_name,c.dl_name)yh,
 (
select e.nf_name,e.jj_name,e.dl_name,
       nvl(sum(decode(b.TYPE_ID,1,a.qty,0)),0) dp_store,
       nvl(sum(decode(b.TYPE_ID,3,a.qty,0)),0) ck_store
  from BI_FA_STORAGE a,BS_C_STORE  b,bi_m_product_all d,bi_m_product_xsfx_all e
 where a.c_store_id=b.id
   and a.m_product_id=d.id
   and d.e_name=e.e_name
   and b.c_customer_id=251
   and a.qty<>0
group by e.nf_name,e.jj_name,e.dl_name)kc,
 (
select b.nf_name,b.jj_name,b.dl_name,nvl(sum(a.pf),0) pf
  from bi_m_saleitem_xsfx a, bi_m_product_xsfx_all b
 where a.e_name = b.e_name
   and a.C_CUSTOMERUP_ID in (251,214)
 group by b.nf_name,b.jj_name,b.dl_name)pf,


 (
select c.m_dim2_id,c.nf_name,c.m_dim9_id,c.jj_name,c.dl_id,c.dl_name,nvl(sum(a.qty),0) ls
from BI_RETAILITEM a,BS_C_STORE b,bi_m_product_xsfx_all c,BI_M_PRODUCT_all d
where a.c_store_id=b.id
  and a.m_product_id=d.id
  and d.e_name=c.e_name
  and b.c_customer_id in (251,214)
group by c.m_dim2_id,c.nf_name,c.m_dim9_id,c.jj_name,c.dl_id,c.dl_name
order by c.m_dim2_id desc,c.m_dim9_id desc)ls,


 (
select c.nf_name,c.jj_name,c.dl_name,
       nvl(sum(case when b.name like ''江西%'' then a.qty end),0) jxlsl,
       nvl(sum(case when b.name like ''新温州%'' then a.qty end),0) wzlsl,
       nvl(sum(case when b.name like ''江西%'' then a.tot_amt_actual end),0) jxlsje,
       nvl(sum(case when b.name like ''新温州%'' then a.tot_amt_actual end),0) wzlsje
from BI_RETAILITEM a,BS_C_STORE b,bi_m_product_xsfx_all c,BI_M_PRODUCT_all d
where a.c_store_id=b.id
  and a.m_product_id=d.id
  and d.e_name=c.e_name
 #   and a.billdate=[billdate] #
  and b.c_customer_id=251
group by c.nf_name,c.jj_name,c.dl_name)dt
where ls.nf_name=yh.nf_name(+)
  and ls.jj_name=yh.jj_name(+)
  and ls.dl_name=yh.dl_name(+)
  and ls.nf_name=kc.nf_name(+)
  and ls.jj_name=kc.jj_name(+)
  and ls.dl_name=kc.dl_name(+)
  and ls.nf_name=pf.nf_name(+)
  and ls.jj_name=pf.jj_name(+)
  and ls.dl_name=pf.dl_name(+)
  and ls.nf_name=dt.nf_name(+)
  and ls.jj_name=dt.jj_name(+)
  and ls.dl_name=dt.dl_name(+)
)
(
select decode(grouping(m_dim2_id)+grouping(m_dim9_id)+grouping(year_season),3,''合计'',year_season)year_season,
       sum(purchase)purchase,
       to_char(round(((sum(sale)+sum(retail))/(sum(surplus_goods)+sum(storehouse)+sum(store)+sum(sale)+sum(retail)))*100,2),''fm9999990.9999'')||''%'' sell_out,
       sum(surplus_goods)surplus_goods,
       sum(storehouse)storehouse,sum(store)store,
       sum(qty_jx)qty_jx,sum(amount_jx)amount_jx,sum(qty_wz)qty_wz,sum(amount_wz)amount_wz,
       sum(sale)sale,sum(retail)retail
from(
select dense_rank() over(order by j.m_dim2_id desc,j.m_dim9_id desc) as rn,
       j.m_dim2_id,j.m_dim9_id,max(j.nf_name||j.jj_name) year_season,
        sum(j.pf)+sum(j.ls)+sum(j.yh)+sum(j.ck_store)+sum(j.dp_store)purchase,
        to_char(round(((sum(j.pf)+sum(j.ls))/(sum(j.pf)+sum(j.ls)+sum(j.yh)+sum(j.ck_store)+sum(j.dp_store)))*100,2),''fm9999990.9999'')||''%'' sell_out,
        sum(j.yh) surplus_goods,sum(j.ck_store)storehouse,sum(j.dp_store)store,
        sum(j.jxlsl)qty_jx,sum(j.jxlsje)amount_jx,sum(j.wzlsl)qty_wz,sum(j.wzlsje)amount_wz,
        sum(j.pf)sale,sum(j.ls)retail
from jxc j
where j.dl_id=240
group by j.m_dim2_id,j.m_dim9_id)
where rn<=8
group by rollup((m_dim2_id,m_dim9_id,year_season))
)
union all
(select ''包包'' year_season,
        sum(j.pf)+sum(j.ls)+sum(j.yh)+sum(j.ck_store)+sum(j.dp_store)purchase,
        to_char(round(((sum(j.pf)+sum(j.ls))/(sum(j.pf)+sum(j.ls)+sum(j.yh)+sum(j.ck_store)+sum(j.dp_store)))*100,2),''fm9999990.9999'')||''%'' sell_out,
        sum(j.yh) surplus_goods,sum(j.ck_store)storehouse,sum(j.dp_store)store,
        sum(j.jxlsl)qty_jx,sum(j.jxlsje)amount_jx,sum(j.wzlsl)qty_wz,sum(j.wzlsje)amount_wz,
        sum(j.pf)sale,sum(j.ls)retail
from jxc j
where j.dl_id=241
  and j.nf_name=to_char(sysdate,''YYYY''))', 'with jxc as
(
select ls.m_dim2_id,ls.nf_name,ls.m_dim9_id,ls.jj_name,ls.dl_id,ls.dl_name,
       nvl(yh.yh,0)yh,nvl(kc.ck_store,0)ck_store,nvl(kc.dp_store,0)dp_store,
       nvl(dt.jxlsl,0)jxlsl,nvl(dt.jxlsje,0)jxlsje,nvl(dt.wzlsl,0)wzlsl,nvl(dt.wzlsje,0)wzlsje,
       nvl(pf.pf,0)pf,nvl(ls.ls,0)ls
from
 (
 select c.nf_name,c.jj_name,c.dl_name,nvl(sum(a.QTYREM),0)yh
 from b_soitem@Portal80 a,BS_C_STORE b,bi_m_product_xsfx_all c,BI_M_PRODUCT_all d,b_so@portal80 e
 where a.b_so_id=e.id
   and e.C_DEST_ID=b.id
   and a.m_product_id=d.id
   and d.e_name=c.e_name
 #    and e.billdate<=[billdate] #
   and b.c_customer_id in (251,214)
   and e.status=2
   and e.isactive=''Y''
 group by c.nf_name,c.jj_name,c.dl_name)yh,
 (
select e.nf_name,e.jj_name,e.dl_name,
       nvl(sum(decode(b.TYPE_ID,1,a.qty,0)),0) dp_store,
       nvl(sum(decode(b.TYPE_ID,3,a.qty,0)),0) ck_store
  from BI_FA_STORAGE a,BS_C_STORE  b,bi_m_product_all d,bi_m_product_xsfx_all e
 where a.c_store_id=b.id
   and a.m_product_id=d.id
   and d.e_name=e.e_name
   and b.c_customer_id=251
   and a.qty<>0
group by e.nf_name,e.jj_name,e.dl_name)kc,
 (
select b.nf_name,b.jj_name,b.dl_name,nvl(sum(a.pf),0) pf
  from bi_m_saleitem_xsfx a, bi_m_product_xsfx_all b
 where a.e_name = b.e_name
   and a.C_CUSTOMERUP_ID in (251,214)
 group by b.nf_name,b.jj_name,b.dl_name)pf,


 (
select c.m_dim2_id,c.nf_name,c.m_dim9_id,c.jj_name,c.dl_id,c.dl_name,nvl(sum(a.qty),0) ls
from BI_RETAILITEM a,BS_C_STORE b,bi_m_product_xsfx_all c,BI_M_PRODUCT_all d
where a.c_store_id=b.id
  and a.m_product_id=d.id
  and d.e_name=c.e_name
  and b.c_customer_id in (251,214)
group by c.m_dim2_id,c.nf_name,c.m_dim9_id,c.jj_name,c.dl_id,c.dl_name
order by c.m_dim2_id desc,c.m_dim9_id desc)ls,


 (
select c.nf_name,c.jj_name,c.dl_name,
       nvl(sum(case when b.name like ''江西%'' then a.qty end),0) jxlsl,
       nvl(sum(case when b.name like ''新温州%'' then a.qty end),0) wzlsl,
       nvl(sum(case when b.name like ''江西%'' then a.tot_amt_actual end),0) jxlsje,
       nvl(sum(case when b.name like ''新温州%'' then a.tot_amt_actual end),0) wzlsje
from BI_RETAILITEM a,BS_C_STORE b,bi_m_product_xsfx_all c,BI_M_PRODUCT_all d
where a.c_store_id=b.id
  and a.m_product_id=d.id
  and d.e_name=c.e_name
 #   and a.billdate=[billdate] #
  and b.c_customer_id=251
group by c.nf_name,c.jj_name,c.dl_name)dt
where ls.nf_name=yh.nf_name(+)
  and ls.jj_name=yh.jj_name(+)
  and ls.dl_name=yh.dl_name(+)
  and ls.nf_name=kc.nf_name(+)
  and ls.jj_name=kc.jj_name(+)
  and ls.dl_name=kc.dl_name(+)
  and ls.nf_name=pf.nf_name(+)
  and ls.jj_name=pf.jj_name(+)
  and ls.dl_name=pf.dl_name(+)
  and ls.nf_name=dt.nf_name(+)
  and ls.jj_name=dt.jj_name(+)
  and ls.dl_name=dt.dl_name(+)
)
(
select decode(grouping(m_dim2_id)+grouping(m_dim9_id)+grouping(year_season),3,''合计'',year_season)year_season,
       sum(purchase)purchase,
       to_char(round(((sum(sale)+sum(retail))/(sum(surplus_goods)+sum(storehouse)+sum(store)+sum(sale)+sum(retail)))*100,2),''fm9999990.9999'')||''%'' sell_out,
       sum(surplus_goods)surplus_goods,
       sum(storehouse)storehouse,sum(store)store,
       sum(qty_jx)qty_jx,sum(amount_jx)amount_jx,sum(qty_wz)qty_wz,sum(amount_wz)amount_wz,
       sum(sale)sale,sum(retail)retail
from(
select dense_rank() over(order by j.m_dim2_id desc,j.m_dim9_id desc) as rn,
       j.m_dim2_id,j.m_dim9_id,max(j.nf_name||j.jj_name) year_season,
        sum(j.pf)+sum(j.ls)+sum(j.yh)+sum(j.ck_store)+sum(j.dp_store)purchase,
        to_char(round(((sum(j.pf)+sum(j.ls))/(sum(j.pf)+sum(j.ls)+sum(j.yh)+sum(j.ck_store)+sum(j.dp_store)))*100,2),''fm9999990.9999'')||''%'' sell_out,
        sum(j.yh) surplus_goods,sum(j.ck_store)storehouse,sum(j.dp_store)store,
        sum(j.jxlsl)qty_jx,sum(j.jxlsje)amount_jx,sum(j.wzlsl)qty_wz,sum(j.wzlsje)amount_wz,
        sum(j.pf)sale,sum(j.ls)retail
from jxc j
where j.dl_id=240
group by j.m_dim2_id,j.m_dim9_id)
where rn<=8
group by rollup((m_dim2_id,m_dim9_id,year_season))
)
union all
(select ''包包'' year_season,
        sum(j.pf)+sum(j.ls)+sum(j.yh)+sum(j.ck_store)+sum(j.dp_store)purchase,
        to_char(round(((sum(j.pf)+sum(j.ls))/(sum(j.pf)+sum(j.ls)+sum(j.yh)+sum(j.ck_store)+sum(j.dp_store)))*100,2),''fm9999990.9999'')||''%'' sell_out,
        sum(j.yh) surplus_goods,sum(j.ck_store)storehouse,sum(j.dp_store)store,
        sum(j.jxlsl)qty_jx,sum(j.jxlsje)amount_jx,sum(j.wzlsl)qty_wz,sum(j.wzlsje)amount_wz,
        sum(j.pf)sale,sum(j.ls)retail
from jxc j
where j.dl_id=241
  and j.nf_name=to_char(sysdate,''YYYY''))', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员零售信息', 'hylsxx', '7', '127', 'select a.billdate,
       a.docno,
       a.c_store_name,
       a.m_product_name,
       a.tot_amt_actual,
       d.attribname
  from ls_bill_d_retail a, vip_bs_vipinfo v,m_dim@portal80 d,
       (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
 where 1 = 1
   and a.c_vip_id = v.vip_id
   and a.m_dim8_id=d.id
   and tx.c_store_id=a.c_store_id
 # and v.mobil = [mobel] #
 order by billdate desc', 'select a.billdate,
       a.docno,
       a.c_store_name,
       a.m_product_name,
       a.tot_amt_actual,
       d.attribname
  from ls_bill_d_retail a, vip_bs_vipinfo v,m_dim@portal80 d
 where 1 = 1
   and a.c_vip_id = v.vip_id
   and a.m_dim8_id=d.id
 # and v.mobil = [mobel] #
 order by billdate desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '权限设置-经销商', 'qxsz-jxs', '13', '128', 'SELECT S.ID, S.NAME,
(case when instr(s.name,''总'')>0 then substr(s.name,0,instr(s.name,''总'')-1) else substr(s.name,0,3) end) name1
  FROM C_CUSTOMER@PORTAL80 S,
  (select * from table(f_store_table(''@fdd'')) where 1 =1) tx
 WHERE s.id=tx.c_customer_id
   and S.C_CUSRANK_ID = 2
   AND S.ISACTIVE = ''Y''
   and s.IS_ONLINE=''Y''
   AND s.name NOT LIKE ''%旧%''
   AND s.name NOT LIKE ''%老%''
   AND s.id NOT IN (15177,15184,15183,177,184,194,197,196,205,212,6075,199,7605,13754,214)
   AND s.description NOT LIKE ''%已停用%''
group by s.id,s.name,
case when instr(s.name,''总'')>0 then substr(s.name,0,instr(s.name,''总'')-1) else substr(s.name,0,3) end
order by name1,name', 'SELECT S.ID, S.NAME,
(case when instr(s.name,''总'')>0 then substr(s.name,0,instr(s.name,''总'')-1) else substr(s.name,0,3) end) name1
  FROM C_CUSTOMER@PORTAL80 S
 WHERE S.C_CUSRANK_ID = 2
   AND S.ISACTIVE = ''Y''
   and s.IS_ONLINE=''Y''
   AND s.name NOT LIKE ''%旧%''
   AND s.name NOT LIKE ''%老%''
   AND s.id NOT IN (15177,15184,15183,177,184,194,197,196,205,212,6075,199,7605,13754,214)
   AND s.description NOT LIKE ''%已停用%''
group by s.id,s.name,
case when instr(s.name,''总'')>0 then substr(s.name,0,instr(s.name,''总'')-1) else substr(s.name,0,3) end
order by name1,name', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '权限设置-店仓', 'qxsz-dc', '13', '129', 'select id,name,c_customer_id
  from c_store@PORTAL80 a
 where a.name not like ''%停%''
   and a.name not like ''%测试%''
 # and a.c_customer_id=[cCustomerId] #
   and a.isactive = ''Y''
   and a.ISRETAIL = ''Y''', 'select id,name,c_customer_id
  from c_store@PORTAL80 a
 where a.name not like ''%停%''
   and a.name not like ''%测试%''
 # and a.c_customer_id=[cCustomerId] #
   and a.isactive = ''Y''
   and a.ISRETAIL = ''Y''', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', 'BI店仓', 'bidc', '13', '130', 'select aaa.*
from table(f_store_table(''@fdd'')) tx ,(
select id,name,c_customer_id
from bs_c_store a
where a.isactive=''Y''
   and type_id=1
and a.name not like ''%测试%''
and a.c_customer_id=''@customerId''
UNION
select id,name,c_customerup_id
from bs_c_store a
where a.isactive=''Y''
   and type_id=1
and a.name not like ''%测试%''
and a.c_customerup_id=''@customerId''
) aaa
 where tx.c_store_id=aaa.id
', 'select id,name,c_customer_id
from bs_c_store a
where a.isactive=''Y''
   and type_id=1
and a.name not like ''%测试%''
and a.c_customer_id=''@customerId''
UNION
select id,name,c_customerup_id
from bs_c_store a
where a.isactive=''Y''
   and type_id=1
and a.name not like ''%测试%''
and a.c_customerup_id=''@customerId''', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '经销商套餐分析-当日', 'jxstcfx-dr', '8', '132', 'select
  customer_id,
  customer,
 # case when [customerId] is not null then store end store, #
  sum(sales_sum) sales_sum,
  sum(tot_amt_actual) tot_amt_actual,
  sum(setmeal_amt_actual) setmeal_amt_actual,
  round(decode(sum(tot_amt_actual),0,0,nvl(sum(setmeal_amt_actual),0)/sum(tot_amt_actual))*100,2) setmeal_proportion,
  sum(setmeal_sum) setmeal_sum,
  sum(setmeal_store_sum) setmeal_store_sum,
  nvl(sum(vous_sum),0) vous_sum,
  sum(vous_joint) vous_joint,
  round(decode(sum(setmeal_sum),0,0,nvl(sum(vous_joint),0)/sum(setmeal_sum))*100,2) vous_joint_reat,
  sum(vous_repurchas) vous_repurchas,
  round(decode(sum(sales_sum),0,0,nvl(sum(vous_repurchas),0)/sum(sales_sum))*100,2) vous_repurchas_reat,
  round(decode(sum(tot_amt_actual),0,0,nvl(sum(vous_repurchas_amt),0)/sum(tot_amt_actual))*100,2) vous_repurchas_proportion,
  sum(not_vip_sum) not_vip_sum,
  nvl(sum(not_vip_amt_actual),0) not_vip_amt_actual,
  round(decode(sum(tot_amt_actual),0,0,nvl(sum(not_vip_amt_actual),0)/sum(tot_amt_actual))*100,2) not_vip_amt_actual_proportion
 from (

select s.name customer,
       s.id customer_id,
       c.store_name store,
       sum(case when (b.qty != 0 or b.TOT_AMT_ACTUAL != 0) and m.M_DIM18_ID IN (240, 241, 479) then b.qty else 0 end) sales_sum,
       sum(case when (b.qty != 0 or b.TOT_AMT_ACTUAL != 0) and m.M_DIM18_ID IN (240, 241, 479) then b.tot_amt_actual else 0 end) tot_amt_actual,
       sum(case when m.name like ''ZS%'' then b.tot_amt_actual else 0 end) setmeal_amt_actual,
       sum(case when m.name like ''ZS%'' then b.qty else 0 end) setmeal_sum,
       count(distinct(case when m.name like ''ZS%'' then c.c_store_id end)) setmeal_store_sum,
       0 vous_sum,
       0 vous_joint,
       sum(case when a.billdate <> v.opencarddate and a.c_vip_id is not null and n.attribname<>''赠品'' then b.qty else 0 end) vous_repurchas,
       sum(case when a.billdate <> v.opencarddate and a.c_vip_id is not null and n.attribname<>''赠品'' then b.TOT_AMT_ACTUAL else 0 end) vous_repurchas_amt,
       count(distinct(case when a.c_vip_id is null then a.id end)) not_vip_sum,
       sum(case when a.c_vip_id is null then b.tot_amt_actual end) not_vip_amt_actual
  from m_retail@portal80      a,
       m_retailitem@portal80  b,
       m_product@portal80     m,
			 M_DIM@portal80         n,
       (select * from table(f_store_table(''@fdd''))) c,
       c_customer@portal80    s,
       c_vip@portal80         v,
       sjfx_customer@portal80 sn
 where a.id = b.m_retail_id
   and b.m_product_id = m.id
	 and m.M_DIM18_ID=n.id
   and a.c_store_id = c.c_store_id
   and c.c_customer_id = sn.id
   and sn.customer_id = s.id
   and a.c_vip_id = v.id(+)
   and a.isactive = ''Y''
 # and sn.customer_id=[customerId] #
   and a.status = 2
   and s.c_cusrank_id = 2
   and m.isactive = ''Y''
   and a.billdate=to_char(sysdate,''YYYYMMDD'')
 group by s.name, s.id, c.store_name

union all

select s.name customer,
       s.id customer_id,
       c.store_name store,
       0 sales_sum,
       0 tot_amt_actual,
       0 setmeal_amt_actual,
       0 setmeal_sum,
       0 setmeal_store_sum,
       nvl(sum(a.vou_sum), 0) vou_sum,
       0 vous_joint,
       0 vous_repurchas,
       0 vous_repurchas_amt,
       0 not_vip_sum,
       0 not_vip_amt_actual
  from  (select a.c_store_id,a.vou_sum
           from m_retail@portal80 a
          where a.vou_sum > 0
            and a.isactive = ''Y''
            and a.billdate=to_char(sysdate,''YYYYMMDD'')
            and a.status = 2
            and a.RCOPY_STATUS = 1) a,
       (select * from table(f_store_table(''@fdd''))) c,
       c_customer@portal80 s,
       sjfx_customer@portal80 sn
 where a.c_store_id(+) = c.c_store_id
   and c.c_customer_id = sn.id(+)
 # and sn.customer_id=[customerId] #
   and sn.customer_id = s.id
   and s.c_cusrank_id = 2
 group by s.name, s.id, c.store_name

union all

 select customer,
       customer_id,
       store store,
       0 sales_sum,
       0 tot_amt_actual,
       0 setmeal_amt_actual,
       0 setmeal_sum,
       0 setmeal_store_sum,
       0 vou_sum,
       nvl(sum(b.qty), 0) vous_joint,
       0 vous_repurchas,
       0 vous_repurchas_amt,
       0 not_vip_sum,
       0 not_vip_amt_actual
  from (select s.name customer,
               s.id customer_id,
               c.store_name store,
               c.store_name name,
               a.c_vip_id,
               a.docno,
               a.id,
               a.vou_sum,
               row_number() over(partition by a.billdate, a.c_vip_id order by a.id) rn
          from m_retail@portal80 a,
               (select * from table(f_store_table(''@fdd''))) c,
               c_customer@portal80 s,
               sjfx_customer@portal80 sn,
               (select a.billdate, a.c_vip_id
                  from m_retail@portal80     a,
                       m_retailitem@portal80 b,
                       m_product@portal80    m,
                       (select c_store_id from table(f_store_table(''@fdd''))) c,
                       c_vip@portal80        v
                 where a.id = b.m_retail_id
                   and b.m_product_id = m.id
                   and a.c_vip_id = v.id
                   and a.c_store_id = c.c_store_id
                   and a.isactive = ''Y''
                   and a.status = 2
                   and a.billdate=to_char(sysdate,''YYYYMMDD'')
                   and m.isactive = ''Y''
                   and m.name like ''ZS%''
                 group by a.billdate, a.c_vip_id
                intersect
                select a.billdate, a.c_vip_id
                  from m_retail@portal80 a,
                       c_vip@portal80    v,
                       (select c_store_id from table(f_store_table(''@fdd''))) c
                 where a.c_vip_id = v.id
                   and a.c_store_id = c.c_store_id
                   and a.status = 2
                   and a.isactive = ''Y''
                   and a.c_vip_id is not null
                   and a.billdate=to_char(sysdate,''YYYYMMDD'')
                 group by a.billdate, a.c_vip_id
                having count(a.id) > 1) aa
         where a.c_store_id = c.c_store_id
           and c.c_customer_id = sn.id
           and sn.customer_id = s.id
         # and sn.customer_id=[customerId] #
           and a.billdate = aa.billdate
           and a.c_vip_id = aa.c_vip_id
           and a.billdate=to_char(sysdate,''YYYYMMDD'')
           and a.isactive = ''Y''
           and s.c_cusrank_id = 2
           and a.status = 2) a,
       M_RETAILITEM@Portal80 b
 where a.id = b.m_retail_id
   and b.TYPE in (1, 4)
   and a.vou_sum > 0
   and rn > 1
 group by customer, customer_id, store

 ) a right outer join
 (select distinct(tf.c_customer_id)c_customer_id from  table(f_store_table(''@fdd'')) tf) b
 on a.customer_id=b.c_customer_id
 where    1=1
and (a.setmeal_amt_actual<>0 or a.vous_joint <>0 or vous_sum<>0 or not_vip_amt_actual<>0 or sales_sum<>0)
 # and c_customer_id=[customerId] #
 group by
  customer_id,customer
  # ,case when [customerId] is not null then store end  #
 order by setmeal_amt_actual desc ', 'select
  customer_id,
  customer,
 # case when [customerId] is not null then store end store, #
  sum(sales_sum) sales_sum,
  sum(tot_amt_actual) tot_amt_actual,
  sum(setmeal_amt_actual) setmeal_amt_actual,
  round(decode(sum(tot_amt_actual),0,0,nvl(sum(setmeal_amt_actual),0)/sum(tot_amt_actual))*100,2) setmeal_proportion,
  sum(setmeal_sum) setmeal_sum,
  sum(setmeal_store_sum) setmeal_store_sum,
  nvl(sum(vous_sum),0) vous_sum,
  sum(vous_joint) vous_joint,
  round(decode(sum(setmeal_sum),0,0,nvl(sum(vous_joint),0)/sum(setmeal_sum))*100,2) vous_joint_reat,
  sum(vous_repurchas) vous_repurchas,
  round(decode(sum(sales_sum),0,0,nvl(sum(vous_repurchas),0)/sum(sales_sum))*100,2) vous_repurchas_reat,
  round(decode(sum(tot_amt_actual),0,0,nvl(sum(vous_repurchas_amt),0)/sum(tot_amt_actual))*100,2) vous_repurchas_proportion,
  sum(not_vip_sum) not_vip_sum,
  nvl(sum(not_vip_amt_actual),0) not_vip_amt_actual,
  round(decode(sum(tot_amt_actual),0,0,nvl(sum(not_vip_amt_actual),0)/sum(tot_amt_actual))*100,2) not_vip_amt_actual_proportion
 from (

select s.name customer,
       s.id customer_id,
       c.name store,
       sum(case when (b.qty != 0 or b.TOT_AMT_ACTUAL != 0) and m.M_DIM18_ID IN (240, 241, 479) then b.qty else 0 end) sales_sum,
       sum(case when (b.qty != 0 or b.TOT_AMT_ACTUAL != 0) and m.M_DIM18_ID IN (240, 241, 479) then b.tot_amt_actual else 0 end) tot_amt_actual,
       sum(case when m.name like ''ZS%'' then b.tot_amt_actual else 0 end) setmeal_amt_actual,
       sum(case when m.name like ''ZS%'' then b.qty else 0 end) setmeal_sum,
       count(distinct(case when m.name like ''ZS%'' then c.c_store_id end)) setmeal_store_sum,
       0 vous_sum,
       0 vous_joint,
       sum(case when a.billdate <> v.opencarddate and a.c_vip_id is not null and n.attribname<>''赠品'' then b.qty else 0 end) vous_repurchas,
       sum(case when a.billdate <> v.opencarddate and a.c_vip_id is not null and n.attribname<>''赠品'' then b.TOT_AMT_ACTUAL else 0 end) vous_repurchas_amt,
       count(distinct(case when a.c_vip_id is null then a.id end)) not_vip_sum,
       sum(case when a.c_vip_id is null then b.tot_amt_actual end) not_vip_amt_actual
  from m_retail@portal80      a,
       m_retailitem@portal80  b,
       m_product@portal80     m,
			 M_DIM@portal80         n,
       c_store@portal80       c,
       c_customer@portal80    s,
       c_vip@portal80         v,
       sjfx_customer@portal80 sn
 where a.id = b.m_retail_id
   and b.m_product_id = m.id
	 and m.M_DIM18_ID=n.id
   and a.c_store_id = c.id
   and c.c_customer_id = sn.id
   and sn.customer_id = s.id
   and a.c_vip_id = v.id(+)
   and a.isactive = ''Y''
 # and sn.customer_id=[customerId] #
   and a.status = 2
   and s.c_cusrank_id = 2
   and m.isactive = ''Y''
   and a.billdate=to_char(sysdate,''YYYYMMDD'')
 group by s.name, s.id, c.name

union all

select s.name customer,
       s.id customer_id,
       c.name store,
       0 sales_sum,
       0 tot_amt_actual,
       0 setmeal_amt_actual,
       0 setmeal_sum,
       0 setmeal_store_sum,
       nvl(sum(a.vou_sum), 0) vou_sum,
       0 vous_joint,
       0 vous_repurchas,
       0 vous_repurchas_amt,
       0 not_vip_sum,
       0 not_vip_amt_actual
  from  (select a.c_store_id,a.vou_sum
           from m_retail@portal80 a
          where a.vou_sum > 0
            and a.isactive = ''Y''
            and a.billdate=to_char(sysdate,''YYYYMMDD'')
            and a.status = 2
            and a.RCOPY_STATUS = 1) a,
       c_store@portal80    c,
       c_customer@portal80 s,
       sjfx_customer@portal80 sn
 where a.c_store_id(+) = c.id
   and c.c_customer_id = sn.id(+)
 # and sn.customer_id=[customerId] #
   and sn.customer_id = s.id
   and s.c_cusrank_id = 2
 group by s.name, s.id, c.name

union all

 select customer,
       customer_id,
       store store,
       0 sales_sum,
       0 tot_amt_actual,
       0 setmeal_amt_actual,
       0 setmeal_sum,
       0 setmeal_store_sum,
       0 vou_sum,
       nvl(sum(b.qty), 0) vous_joint,
       0 vous_repurchas,
       0 vous_repurchas_amt,
       0 not_vip_sum,
       0 not_vip_amt_actual
  from (select s.name customer,
               s.id customer_id,
               c.name store,
               c.name name,
               a.c_vip_id,
               a.docno,
               a.id,
               a.vou_sum,
               row_number() over(partition by a.billdate, a.c_vip_id order by a.id) rn
          from m_retail@portal80 a,
               c_store@PORTAL80 c,
               c_customer@portal80 s,
               sjfx_customer@portal80 sn,
               (select a.billdate, a.c_vip_id
                  from m_retail@portal80     a,
                       m_retailitem@portal80 b,
                       m_product@portal80    m,
                       c_store@PORTAL80 c,
                       c_vip@portal80        v
                 where a.id = b.m_retail_id
                   and b.m_product_id = m.id
                   and a.c_vip_id = v.id
                   and a.c_store_id = c.id
                   and a.isactive = ''Y''
                   and a.status = 2
                   and a.billdate=to_char(sysdate,''YYYYMMDD'')
                   and m.isactive = ''Y''
                   and m.name like ''ZS%''
                 group by a.billdate, a.c_vip_id
                intersect
                select a.billdate, a.c_vip_id
                  from m_retail@portal80 a,
                       c_vip@portal80    v,
                       c_store@PORTAL80 c
                 where a.c_vip_id = v.id
                   and a.c_store_id = c.id
                   and a.status = 2
                   and a.isactive = ''Y''
                   and a.c_vip_id is not null
                   and a.billdate=to_char(sysdate,''YYYYMMDD'')
                 group by a.billdate, a.c_vip_id
                having count(a.id) > 1) aa
         where a.c_store_id = c.id
           and c.c_customer_id = sn.id
           and sn.customer_id = s.id
         # and sn.customer_id=[customerId] #
           and a.billdate = aa.billdate
           and a.c_vip_id = aa.c_vip_id
           and a.billdate=to_char(sysdate,''YYYYMMDD'')
           and a.isactive = ''Y''
           and s.c_cusrank_id = 2
           and a.status = 2) a,
       M_RETAILITEM@Portal80 b
 where a.id = b.m_retail_id
   and b.TYPE in (1, 4)
   and a.vou_sum > 0
   and rn > 1
 group by customer, customer_id, store

 ) a
 where    1=1
and (a.setmeal_amt_actual<>0 or a.vous_joint <>0 or vous_sum<>0 or not_vip_amt_actual<>0 or sales_sum<>0)
 group by
  customer_id,customer
  # ,case when [customerId] is not null then store end  #
 order by setmeal_amt_actual desc ', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('80', '员工性别比例', 'ygxbbl', '9', '135', 'select a.sex name,count(1) value from t_employee a
where a.sex is not null and a.sex <>''''
group by a.sex', 'select a.sex name,count(1) value from t_employee a
where a.sex is not null and a.sex <>''''
group by a.sex', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '客单价分析', 'kdjfx', '8', '133', 'select s.id customer_id,
       s.name customer_name,
  # (case when [customerId] is not null then c.name end) store_name, #
       m.e_name product_name,
       sum(a.qty) qty,
       max(m9.attribname) season,
       sum(case
             when 1=2 then a.tot_amt_actual
           # when m.m_dim9_id=37 and a.tot_amt_actual=0 then to_number([spring]) #
           # when m.m_dim9_id=38 and a.tot_amt_actual=0 then to_number([summer]) #
           # when m.m_dim9_id=39 and a.tot_amt_actual=0 then to_number([autumn]) #
           # when m.m_dim9_id=40 and a.tot_amt_actual=0 then to_number([winter]) #
           # when m.m_dim9_id=666 and a.tot_amt_actual=0 then to_number([other]) #
       else a.tot_amt_actual end) tot_amt_actual,
       round(decode(sum(qty),0,0,sum(case
             when 1=2 then a.tot_amt_actual
           # when m.m_dim9_id=37 and a.tot_amt_actual=0 then to_number([spring]) #
           # when m.m_dim9_id=38 and a.tot_amt_actual=0 then to_number([summer]) #
           # when m.m_dim9_id=39 and a.tot_amt_actual=0 then to_number([autumn]) #
           # when m.m_dim9_id=40 and a.tot_amt_actual=0 then to_number([winter]) #
           # when m.m_dim9_id=666 and a.tot_amt_actual=0 then to_number([other]) #
       else a.tot_amt_actual end)/sum(qty)),2) kdj
  from BI_RETAILITEM  a,
       bi_m_product   m,
       bi_c_store     c,
       m_dim@portal80 m9,
       bi_c_customer  s,
 (select * from table(f_store_table(''@fdd'')) tf) t
 where a.m_product_id = m.id
   and a.c_store_id = c.id
   and m9.id = m.m_dim9_id
   and c.c_customer_id = s.id
   and t.c_store_id=c.id
 # and m.e_name like ''%''||trim([eName]) #
 # and a.billdate between [beginDate] #
 # and [endDate] #
 group by s.id, s.name
  # ,case when [customerId] is not null then c.name end #
  ,m.e_name
 order by tot_amt_actual desc', 'select s.id customer_id,
       s.name customer_name,
  # (case when [customerId] is not null then c.name end) store_name, #
       m.e_name product_name,
       sum(a.qty) qty,
       max(m9.attribname) season,
       sum(case
             when 1=2 then a.tot_amt_actual
           # when m.m_dim9_id=37 and a.tot_amt_actual=0 then to_number([spring]) #
           # when m.m_dim9_id=38 and a.tot_amt_actual=0 then to_number([summer]) #
           # when m.m_dim9_id=39 and a.tot_amt_actual=0 then to_number([autumn]) #
           # when m.m_dim9_id=40 and a.tot_amt_actual=0 then to_number([winter]) #
           # when m.m_dim9_id=666 and a.tot_amt_actual=0 then to_number([other]) #
       else a.tot_amt_actual end) tot_amt_actual,
       round(decode(sum(qty),0,0,sum(case
             when 1=2 then a.tot_amt_actual
           # when m.m_dim9_id=37 and a.tot_amt_actual=0 then to_number([spring]) #
           # when m.m_dim9_id=38 and a.tot_amt_actual=0 then to_number([summer]) #
           # when m.m_dim9_id=39 and a.tot_amt_actual=0 then to_number([autumn]) #
           # when m.m_dim9_id=40 and a.tot_amt_actual=0 then to_number([winter]) #
           # when m.m_dim9_id=666 and a.tot_amt_actual=0 then to_number([other]) #
       else a.tot_amt_actual end)/sum(qty)),2) kdj
  from BI_RETAILITEM  a,
       bi_m_product   m,
       bi_c_store     c,
       m_dim@portal80 m9,
       bi_c_customer  s
 where a.m_product_id = m.id
   and a.c_store_id = c.id
   and m9.id = m.m_dim9_id
   and c.c_customer_id = s.id
 # and m.e_name like ''%''||trim([eName]) #
 # and a.billdate between [beginDate] #
 # and [endDate] #
 group by s.id, s.name
  # ,case when [customerId] is not null then c.name end #
  ,m.e_name
 order by tot_amt_actual desc', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('80', '员工离职率查询-年', 'yglzlcx-n', '9', '134', 'select aa.year,aa.start_qty rzsl,aa.at_qty rzzsl,
	   isnull(bb.out_qty,0) lzsl,isnull(bb.end_qty,0) lzzsl,
	   aa.at_qty-isnull(bb.end_qty,0) zzzsl,
	   round(cast(isnull(bb.out_qty,0) as float)/(aa.at_qty-isnull(bb.end_qty,0)),4)*100
	    lzl
from (
select substring(a.startdate,1,4) year,count(1) start_qty,
	   sum(convert(int,count(1))) over(order by substring(a.startdate,1,4)) at_qty
from t_employee a
where a.startdate is not null and a.startdate <>''''
group by substring(a.startdate,1,4)
) aa
left join
(
select substring(a.outdate,1,4) year,count(1) out_qty,
	   sum(convert(int,count(1))) over(order by substring(a.outdate,1,4)) end_qty
from t_employee a
where a.outdate is not null and a.outdate <>''''
group by substring(a.outdate,1,4)
) bb on aa.year=bb.year
where aa.year=2019
order by aa.year', 'select aa.year,aa.start_qty rzsl,aa.at_qty rzzsl,
	   isnull(bb.out_qty,0) lzsl,isnull(bb.end_qty,0) lzzsl,
	   aa.at_qty-isnull(bb.end_qty,0) zzzsl,
	   round(cast(isnull(bb.out_qty,0) as float)/(aa.at_qty-isnull(bb.end_qty,0)),4)*100
	    lzl
from (
select substring(a.startdate,1,4) year,count(1) start_qty,
	   sum(convert(int,count(1))) over(order by substring(a.startdate,1,4)) at_qty
from t_employee a
where a.startdate is not null and a.startdate <>''''
group by substring(a.startdate,1,4)
) aa
left join
(
select substring(a.outdate,1,4) year,count(1) out_qty,
	   sum(convert(int,count(1))) over(order by substring(a.outdate,1,4)) end_qty
from t_employee a
where a.outdate is not null and a.outdate <>''''
group by substring(a.outdate,1,4)
) bb on aa.year=bb.year
where aa.year=2019
order by aa.year', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('80', '员工年龄比例', 'ygnlbl', '9', '136', '   select CONCAT(aa.age, ''0后'') name,
          count(1) value
     from (select cast(substring(BIRTHDAY, 3, 1) as int) age
             from t_employee a
            where a.birthday is not null
              and a.birthday <> '''') aa
    group by aa.age', '   select CONCAT(aa.age, ''0后'') name,
          count(1) value
     from (select cast(substring(BIRTHDAY, 3, 1) as int) age
             from t_employee a
            where a.birthday is not null
              and a.birthday <> '''') aa
    group by aa.age', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('80', '员工地域分部', 'ygdyfb', '9', '137', 'select substring(address, 0, charindex(''省'', address, 0) ) name,
       count(1) qty
  from t_employee a
 where (a.outdate is null or a.outdate = '''')
   and a.address like ''%省%''
 group by substring(address, 0, charindex(''省'', address, 0) )', 'select substring(address, 0, charindex(''省'', address, 0) ) name,
       count(1) qty
  from t_employee a
 where (a.outdate is null or a.outdate = '''')
   and a.address like ''%省%''
 group by substring(address, 0, charindex(''省'', address, 0) )', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('80', '员工离职原因分析', 'yglzyyfx', '9', '138', ' select substring(leavecause, 0, 10) name, count(1) value
   from t_employee a
  where a.leavecause is not null
    and a.leavecause <> ''''
  group by substring(leavecause, 0, 10)', ' select substring(leavecause, 0, 10) name, count(1) value
   from t_employee a
  where a.leavecause is not null
    and a.leavecause <> ''''
  group by substring(leavecause, 0, 10)', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('80', '员工学历分析', 'ygxlfx', '9', '140', '   select degree name, count(1) value
     from t_employee a
    where a.DEGREE is not null
      and a.DEGREE <> ''''
    group by degree', '   select degree name, count(1) value
     from t_employee a
    where a.DEGREE is not null
      and a.DEGREE <> ''''
    group by degree', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '新全国日销TOP30', 'xqgrxtop30', '10', '141', 'select day_top,customer,store_name,day_tot_amt_actual,day_retail_tot_amt_actual,
       day_retail_tot_amt_actual1,day_recharge_tot_amt_actual,day_qty,day_retail_qty,
       day_retail_qty1,day_avg_price,day_avg_price1,mon_tot_amt_actual,
       mon_retail_tot_amt_actual,mon_recharge_tot_amt_actual,mon_qty,mon_retail_qty,
       mon_recharge_qty,mon_avg_price,mon_retail_avg_price,mon_recharge_avg_price,
       year_storage,year_storage1,year_storage2
from(
SELECT row_number() over(order by a.tot_amt_actual desc)day_top,
       a.customer_name customer,
       a.store_name store_name,
       round(a.tot_amt_actual) day_tot_amt_actual,
       round(a.nozs_tot_amt_actual) day_retail_tot_amt_actual,
       round(a.nozs_tot_amt_actual1) day_retail_tot_amt_actual1,
       round(a.zs_tot_amt_actual) day_recharge_tot_amt_actual,
       round(a.qty) day_qty,
       a.nozs_qty day_retail_qty,
       a.nozs_qty1 day_retail_qty1,
       round(decode(a.qty,0,0,a.tot_amt_actual/a.qty)) day_avg_price,
       round(decode(a.nozs_qty1,0,0,a.nozs_tot_amt_actual1/a.nozs_qty1)) day_avg_price1,
       b.tot_amt_actual_m mon_tot_amt_actual,
       b.nozs_tot_amt_actual_m mon_retail_tot_amt_actual,
       b.zs_tot_amt_actual_m mon_recharge_tot_amt_actual,
       b.qty_m mon_qty,
       b.nozs_qty_m mon_retail_qty,
       b.zs_qty_m mon_recharge_qty,
       round(decode(b.mon_day_count,0,0,b.tot_amt_actual_m/b.mon_day_count)) mon_avg_price,
       round(decode(b.mon_retail_day_count,0,0,b.nozs_tot_amt_actual_m/b.mon_retail_day_count)) mon_retail_avg_price,
       round(decode(b.mon_recharge_day_count,0,0,b.zs_tot_amt_actual_m/b.mon_recharge_day_count)) mon_recharge_avg_price,
       year_storage,year_storage1,year_storage2
FROM
(select a.c_customer_id,a.customer_name,a.c_store_id,a.store_name,
       a.tot_amt_actual,a.nozs_tot_amt_actual,a.nozs_tot_amt_actual1,a.zs_tot_amt_actual,
       a.qty,a.nozs_qty,a.nozs_qty1,a.zs_qty
from xsfx_ls_d_store_zs a,table(f_store_table(''@fdd''))t
where a.c_store_id=t.c_store_id
 #   and a.billdate=[billdate])A, #

(select a.c_customer_id,a.customer_name,a.c_store_id,a.store_name,
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.tot_amt_actual else 0 end)tot_amt_actual_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.nozs_tot_amt_actual else 0 end )nozs_tot_amt_actual_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.nozs_tot_amt_actual1 else 0 end)nozs_tot_amt_actual1_a, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.zs_tot_amt_actual else 0 end)zs_tot_amt_actual_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.qty else 0 end)qty_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.nozs_qty else 0 end)nozs_qty_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.nozs_qty1 else 0 end)nozs_qty1_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.zs_qty else 0 end)zs_qty_m, #
 #        count(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                         [billdate] then a.billdate else 0 end) mon_day_count, #
 #        count(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                         [billdate] and a.nozs_qty>0 then a.billdate else 0 end) mon_retail_day_count, #
 #        count(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                         [billdate] and a.zs_qty>0 then a.billdate else 0 end) mon_recharge_day_count #
from xsfx_ls_d_store_zs a,t_day b,table(f_store_table(''@fdd''))t
where a.billdate=b.id
  and a.c_store_id=t.c_store_id
 #   and b.t_month_id=substr([billdate],0,6) #
group by a.c_customer_id,a.customer_name,a.c_store_id,a.store_name)B,

(select a.c_store_id,
sum(case
 #   when a.year_name = to_number(to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''yyyy'')) #
      and a.season_name =
 #  (case when to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''mm'') in (''12'',''01'',''02'') then ''春'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''mm'') in (''03'',''04'',''05'') then ''夏'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''mm'') in (''06'',''07'',''08'') then ''秋'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''mm'') in (''09'',''10'',''11'') then ''冬'' #
     end) then qty else   0 end) year_storage,
sum(case
 #  when a.year_name = to_number(to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''yyyy'')) #
     and a.season_name =
 #  (case when to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''mm'') in (''12'',''01'',''02'') then ''春'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''mm'') in (''03'',''04'',''05'') then ''夏'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''mm'') in (''06'',''07'',''08'') then ''秋'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''mm'') in (''09'',''10'',''11'') then ''冬'' #
     end) then qty else  0 end) year_storage1,
sum(case
 #   when a.year_name = to_number(to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''yyyy'')) #
     and a.season_name =
 #  (case when to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''mm'') in (''12'',''01'',''02'') then ''春'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''mm'') in (''03'',''04'',''05'') then ''夏'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''mm'') in (''06'',''07'',''08'') then ''秋'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''mm'') in (''09'',''10'',''11'') then ''冬'' #
     end) then qty else 0 end) year_storage2
from storage_yearseason a,table(f_store_table(''@fdd''))t
where a.c_store_id=t.c_store_id
group by a.c_store_id)C
WHERE A.C_STORE_ID=B.C_STORE_ID
  AND A.C_STORE_ID=C.C_STORE_ID)
where day_top<=30', 'select day_top,customer,store_name,day_tot_amt_actual,day_retail_tot_amt_actual,
       day_retail_tot_amt_actual1,day_recharge_tot_amt_actual,day_qty,day_retail_qty,
       day_retail_qty1,day_avg_price,day_avg_price1,mon_tot_amt_actual,
       mon_retail_tot_amt_actual,mon_recharge_tot_amt_actual,mon_qty,mon_retail_qty,
       mon_recharge_qty,mon_avg_price,mon_retail_avg_price,mon_recharge_avg_price,
       year_storage,year_storage1,year_storage2
from(
SELECT row_number() over(order by a.tot_amt_actual desc)day_top,
       a.customer_name customer,
       a.store_name store_name,
       round(a.tot_amt_actual) day_tot_amt_actual,
       round(a.nozs_tot_amt_actual) day_retail_tot_amt_actual,
       round(a.nozs_tot_amt_actual1) day_retail_tot_amt_actual1,
       round(a.zs_tot_amt_actual) day_recharge_tot_amt_actual,
       round(a.qty) day_qty,
       a.nozs_qty day_retail_qty,
       a.nozs_qty1 day_retail_qty1,
       round(decode(a.qty,0,0,a.tot_amt_actual/a.qty)) day_avg_price,
       round(decode(a.nozs_qty1,0,0,a.nozs_tot_amt_actual1/a.nozs_qty1)) day_avg_price1,
       b.tot_amt_actual_m mon_tot_amt_actual,
       b.nozs_tot_amt_actual_m mon_retail_tot_amt_actual,
       b.zs_tot_amt_actual_m mon_recharge_tot_amt_actual,
       b.qty_m mon_qty,
       b.nozs_qty_m mon_retail_qty,
       b.zs_qty_m mon_recharge_qty,
       round(decode(b.mon_day_count,0,0,b.tot_amt_actual_m/b.mon_day_count)) mon_avg_price,
       round(decode(b.mon_retail_day_count,0,0,b.nozs_tot_amt_actual_m/b.mon_retail_day_count)) mon_retail_avg_price,
       round(decode(b.mon_recharge_day_count,0,0,b.zs_tot_amt_actual_m/b.mon_recharge_day_count)) mon_recharge_avg_price,
       year_storage,year_storage1,year_storage2
FROM
(select a.c_customer_id,a.customer_name,a.c_store_id,a.store_name,
       a.tot_amt_actual,a.nozs_tot_amt_actual,a.nozs_tot_amt_actual1,a.zs_tot_amt_actual,
       a.qty,a.nozs_qty,a.nozs_qty1,a.zs_qty
from xsfx_ls_d_store_zs a
 # where a.billdate=[billdate])A, #

(select a.c_customer_id,a.customer_name,a.c_store_id,a.store_name,
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                     [billdate] then a.tot_amt_actual else 0 end)tot_amt_actual_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                     [billdate] then a.nozs_tot_amt_actual else 0 end )nozs_tot_amt_actual_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                     [billdate] then a.nozs_tot_amt_actual1 else 0 end)nozs_tot_amt_actual1_a, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                     [billdate] then a.zs_tot_amt_actual else 0 end)zs_tot_amt_actual_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                     [billdate] then a.qty else 0 end)qty_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                     [billdate] then a.nozs_qty else 0 end)nozs_qty_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                     [billdate] then a.nozs_qty1 else 0 end)nozs_qty1_m, #
 #        sum(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                     [billdate] then a.zs_qty else 0 end)zs_qty_m, #
 #        count(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] then a.billdate else 0 end) mon_day_count, #
 #        count(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] and a.nozs_qty>0 then a.billdate else 0 end) mon_retail_day_count, #
 #        count(case when a.billdate between substr([billdate],0,6)||''01'' and #
 #                       [billdate] and a.zs_qty>0 then a.billdate else 0 end) mon_recharge_day_count #
from xsfx_ls_d_store_zs a,t_day b
where a.billdate=b.id
 #   and b.t_month_id=substr([billdate],0,6) #
group by a.c_customer_id,a.customer_name,a.c_store_id,a.store_name)B,

(select a.c_store_id,
sum(case
 #   when a.year_name = to_number(to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''yyyy'')) #
      and a.season_name =
 #  (case when to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''mm'') in (''12'',''01'',''02'') then ''春'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''mm'') in (''03'',''04'',''05'') then ''夏'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''mm'') in (''06'',''07'',''08'') then ''秋'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -6), ''mm'') in (''09'',''10'',''11'') then ''冬'' #
     end) then qty else   0 end) year_storage,
sum(case
 #  when a.year_name = to_number(to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''yyyy'')) #
     and a.season_name =
 #  (case when to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''mm'') in (''12'',''01'',''02'') then ''春'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''mm'') in (''03'',''04'',''05'') then ''夏'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''mm'') in (''06'',''07'',''08'') then ''秋'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), -3), ''mm'') in (''09'',''10'',''11'') then ''冬'' #
     end) then qty else  0 end) year_storage1,
sum(case
 #   when a.year_name = to_number(to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''yyyy'')) #
     and a.season_name =
 #  (case when to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''mm'') in (''12'',''01'',''02'') then ''春'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''mm'') in (''03'',''04'',''05'') then ''夏'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''mm'') in (''06'',''07'',''08'') then ''秋'' #
 #        when to_char(add_months(to_date([billdate],''YYYYMMDD''), 0), ''mm'') in (''09'',''10'',''11'') then ''冬'' #
     end) then qty else 0 end) year_storage2
from storage_yearseason a
group by a.c_store_id)C
WHERE A.C_STORE_ID=B.C_STORE_ID
  AND A.C_STORE_ID=C.C_STORE_ID)
where day_top<=30', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '会员购买次数', 'hygmcs', '16', '119', 'select case
         when qty = 1 then ''1次''
         when qty = 2 then ''2次''
         when qty = 3 then ''3次''
         else ''大于3次''
       end name,
       count(1) qty
  from (select c_vip_id, count(1) qty
  from (select c_vip_id, docno
          from ls_bill_d_retail a,BIFX_M_PRODUCT b,
  (select * from table(f_store_table(''@fdd'')) where 1 =1  ) tx
         where 1 = 1
   and a.c_store_id=tx.c_store_id
  and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.status = 2
           and a.isactive = ''Y''
           and a.c_vip_id is not null
         group by c_vip_id, docno)
 group by c_vip_id)
 group by case
            when qty = 1 then ''1次''
            when qty = 2 then ''2次''
            when qty = 3 then ''3次''
            else ''大于3次''
          end', 'select case
         when qty = 1 then ''1次''
         when qty = 2 then ''2次''
         when qty = 3 then ''3次''
         else ''大于3次''
       end name,
       count(1) qty
  from (select c_vip_id, count(1) qty
  from (select c_vip_id, docno
          from ls_bill_d_retail a,BIFX_M_PRODUCT b
         where 1 = 1
and a.m_product_id=b.id
 # and a.billdate between [beginDate] #
 # and [endDate] #
 #  and a.billdate <= [jzDate] #
 #  and a.billdate between [qsDate] and to_char(sysdate-1,''YYYYMMDD'') #
   # AND a.c_store_id IN (WITH GS AS (SELECT [storeIds] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND season_id IN (WITH GS AS (SELECT [season] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
   # AND m_dim2_id IN (WITH GS AS (SELECT [year] AS TEXT FROM DUAL) SELECT REGEXP_SUBSTR(TEXT, ''[^,]+'', 1, LEVEL, ''i'') AS TEXT FROM GS CONNECT BY LEVEL <= LENGTH(TEXT) - LENGTH(REPLACE(TEXT, '','')) + 1) #
           and a.billdate between 20180101 and to_char(sysdate-1,''YYYYMMDD'')
           and a.status = 2
           and a.isactive = ''Y''
           and a.c_vip_id is not null
         group by c_vip_id, docno)
 group by c_vip_id)
 group by case
            when qty = 1 then ''1次''
            when qty = 2 then ''2次''
            when qty = 3 then ''3次''
            else ''大于3次''
          end
', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('90', '查询基础表test', 'cxjcbtest', '17', '161', 'select top 10 * from gmq_tk_planstocka', 'select top 10 * from gmq_tk_planstocka', NULL, NULL, NULL, NULL, NULL);
INSERT INTO "SYS_DATA_SEARCH"("DATA_SOURCE_ID", "SEARCH_NAME", "SEARCH_NAME_EN", "SEARCH_TREE_ID", "DATA_SEARCH_ID", "SQL", "ADMIN_SQL", "CREATE_BY", "CREATE_TIME", "UPDATE_BY", "UPDATE_TIME", "REMARK") VALUES ('50', '业绩达成同比分析', 'yjdctbfx', '12', '149', 'select decode(grouping(area),1,''汇总合计'',area)area,
       franchise_mode,
       id,
       decode(grouping(franchise_mode)+grouping(name),1,franchise_mode,2,area||''合计'',name)name,
       sum(tot_amt_mark)target,
			 decode(grouping(area)+grouping(franchise_mode)+grouping(name),0,sum(tot_amt_acc_now),
			        1,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end),
							2,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end),
							3,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end))sales,
			 sum(tot_amt_acc_now) tot_amt_acc_now,
       decode(grouping(area)+grouping(franchise_mode)+grouping(name),0,sum(tot_amt_acc_last),
			        1,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),
							2,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),
							3,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end))same_period,
       round(decode(sum(tot_amt_mark),0,0,sum(tot_amt_acc_now)/sum(tot_amt_mark))*100) reach,
       decode(grouping(area)+grouping(franchise_mode)+grouping(name),0,round(decode(sum(tot_amt_acc_last),0,0,sum(tot_amt_acc_now)/sum(tot_amt_acc_last)-1)*100),
			 1,round(decode(sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),0,0,
			 sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end)/sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end)-1)*100),
			 2,round(decode(sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),0,0,
			 sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end)/sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end)-1)*100),
			 3,round(decode(sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),0,0,
			 sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end)/sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end)-1)*100)) year_on_year,
       decode(grouping(area)+grouping(franchise_mode)+grouping(name),
       0,row_number() over(partition by area,franchise_mode order by sum(tot_amt_mark)desc,sum(tot_amt_acc_now)desc,sum(tot_amt_acc_last)desc,id desc)-1,
       1,count(name),2,null,3,null)rn
from(
select bs.id,bs.name,
       case bs.Franchise_Mode
            when ''a'' then ''加盟''
            when ''b'' then ''直营''
            when ''c'' then ''承包''
            when ''d'' then ''联营'' else ''其他'' end Franchise_Mode,
       case when bs.name like ''江西%'' then ''江西区域''
            when bs.name like ''新温州%'' then ''温州区域'' end area,
       nvl(zb.tot_amt_mark,0) tot_amt_mark,
       nvl(yj.tot_amt_acc_now,0) tot_amt_acc_now,
       nvl(yj.tot_amt_acc_last,0) tot_amt_acc_last
from
BI_C_STORE bs,
(select a.c_store_id,nvl(a.tot_amt_mark,0)tot_amt_mark
from C_STOREMARK a
 # where a.yearmonth= [yearmonth]) zb, #
(select a.c_store_id,
 #         sum(case when [yearmonth]=to_char(sysdate,''yyyymm'')  #
                    then (case when a.billdate between to_char(sysdate,''yyyymm'')||''01'' and to_char(sysdate-1,''yyyymmdd'') then a.tot_amt_acc else 0 end)
 #                  else (case when t.t_month_id=[yearmonth] then a.tot_amt_acc else 0 end) end) tot_amt_acc_now, #
 #         sum(case when to_char(sysdate,''yyyymm'')=[yearmonth]  #
                    then (case when a.billdate between to_char(add_months(sysdate,-12),''yyyymm'')||''01'' and to_char(add_months(sysdate,-12)-1,''yyyymmdd'') then a.tot_amt_acc else 0 end)
 #                  else (case when t.t_month_id=to_char(add_months(to_date([yearmonth],''YYYYMM''),-12),''YYYYMM'') then a.tot_amt_acc end) end)  tot_amt_acc_last #
from LS_BILL_PAYWAY_D_STORE a,t_day t
where a.billdate=t.id
  and a.c_payway_id not in(125)
group by a.c_store_id
) yj

where bs.id=zb.c_store_id(+)
  and bs.id=yj.c_store_id(+)
  and bs.c_customer_id=251) aa
group by rollup (area,franchise_mode,(id,name))', 'select decode(grouping(area),1,''汇总合计'',area)area,
       franchise_mode,
       id,
       decode(grouping(franchise_mode)+grouping(name),1,franchise_mode,2,area||''合计'',name)name,
       sum(tot_amt_mark)target,
			 decode(grouping(area)+grouping(franchise_mode)+grouping(name),0,sum(tot_amt_acc_now),
			        1,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end),
							2,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end),
							3,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end))sales,
			 sum(tot_amt_acc_now) tot_amt_acc_now,
       decode(grouping(area)+grouping(franchise_mode)+grouping(name),0,sum(tot_amt_acc_last),
			        1,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),
							2,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),
							3,sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end))same_period,
       round(decode(sum(tot_amt_mark),0,0,sum(tot_amt_acc_now)/sum(tot_amt_mark))*100) reach,
       decode(grouping(area)+grouping(franchise_mode)+grouping(name),0,round(decode(sum(tot_amt_acc_last),0,0,sum(tot_amt_acc_now)/sum(tot_amt_acc_last)-1)*100),
			 1,round(decode(sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),0,0,
			 sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end)/sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end)-1)*100),
			 2,round(decode(sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),0,0,
			 sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end)/sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end)-1)*100),
			 3,round(decode(sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end),0,0,
			 sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_now else 0 end)/sum(case when tot_amt_acc_now<>0 and tot_amt_acc_last<>0 then tot_amt_acc_last else 0 end)-1)*100)) year_on_year,
       decode(grouping(area)+grouping(franchise_mode)+grouping(name),
       0,row_number() over(partition by area,franchise_mode order by sum(tot_amt_mark)desc,sum(tot_amt_acc_now)desc,sum(tot_amt_acc_last)desc,id desc)-1,
       1,count(name),2,null,3,null)rn
from(
select bs.id,bs.name,
       case bs.Franchise_Mode
            when ''a'' then ''加盟''
            when ''b'' then ''直营''
            when ''c'' then ''承包''
            when ''d'' then ''联营'' else ''其他'' end Franchise_Mode,
       case when bs.name like ''江西%'' then ''江西区域''
            when bs.name like ''新温州%'' then ''温州区域'' end area,
       nvl(zb.tot_amt_mark,0) tot_amt_mark,
       nvl(yj.tot_amt_acc_now,0) tot_amt_acc_now,
       nvl(yj.tot_amt_acc_last,0) tot_amt_acc_last
from
BI_C_STORE bs,
(select a.c_store_id,nvl(a.tot_amt_mark,0)tot_amt_mark
from C_STOREMARK a
 # where a.yearmonth= [yearmonth]) zb, #
(select a.c_store_id,
 #         sum(case when [yearmonth]=to_char(sysdate,''yyyymm'')  #
                    then (case when a.billdate between to_char(sysdate,''yyyymm'')||''01'' and to_char(sysdate-1,''yyyymmdd'') then a.tot_amt_acc else 0 end)
 #                  else (case when t.t_month_id=[yearmonth] then a.tot_amt_acc else 0 end) end) tot_amt_acc_now, #
 #         sum(case when to_char(sysdate,''yyyymm'')=[yearmonth]  #
                    then (case when a.billdate between to_char(add_months(sysdate,-12),''yyyymm'')||''01'' and to_char(add_months(sysdate,-12)-1,''yyyymmdd'') then a.tot_amt_acc else 0 end)
 #                  else (case when t.t_month_id=to_char(add_months(to_date([yearmonth],''YYYYMM''),-12),''YYYYMM'') then a.tot_amt_acc end) end)  tot_amt_acc_last #
from LS_BILL_PAYWAY_D_STORE a,t_day t
where a.billdate=t.id
  and a.c_payway_id not in(125)
group by a.c_store_id
) yj

where bs.id=zb.c_store_id(+)
  and bs.id=yj.c_store_id(+)
  and bs.c_customer_id=251) aa
group by rollup (area,franchise_mode,(id,name))', NULL, NULL, NULL, NULL, NULL);
